Javascript - User Data

Asked

Viewed 893 times

-1

Good afternoon! I’m tending to solve the exercise below but I’m not getting it, I set the code but it didn’t work, could help me?

In a gym there is a register of several users containing the following information: name, age and height. And to perform a particular training, there are some requirements: To be 18 years old or older and to have a height equal to or greater than 1.70.

Create a function called highAlt that returns true (true) if it meets the requirements, and false (false) otherwise. In this function you will receive a parameter that will be an array, containing in the first position the name, second position the age of the student and in the third containing the height in Centimeters.

Example

maiorAlto(["Aluno incrível", 18, 170]) // retorna true
maiortAlto(["Aluno baixo", 17, 150]) // retorna false

Tips:Remember that to create a function we use the word Function In the information array, the user’s age comes first, according to the height (which is in centimeters) To access the position of an array hit put the name followed by square brackets: array[1]

Down with the code I made:

var usuario2 = ['Pessoa do Pântano', 39, 198]
var usuario3 = ['Homem da Lua Virada', 21, 149]
var usuario4 = ['Pequena Paulistana', 18, 171]
var usuario5 = ['Menino da Porteira', 13, 142]

function maiorAlto(["nome", 18, 170]) {

    for (var i = 0; i < maiorAlto.length; i++) {

        if (usuario1[2] == 18 && usuario1[3] == 170)

            return true; false

        if (usuario2[2] == 18 && usuario2[3] == 170)

            return true; false
        if (usuario3[2] == 18 && usuario3[3] == 170)

            return true; false
        if (usuario4[2] == 18 && usuario4[3] == 170)

            return true; false
        if (usuario5[2] == 18 && usuario5[3] == 170)

            return true; false
    }
}

3 answers

1

In the statement informs that the function receives array:

In this function you will receive a parameter that will be an array

Higher Function(array){ }

(the parameter can be any name and not necessarily the name array, is?)

It takes >= 18 years and >= 170, we know that the age position is [1] and the height is [2], so the function will look like this:

function maiorAlto(array){
    if(array[1] >=18 && array[2] >=170){
        return true
    }else{
        return false
    }
}

0

var usuario1 = ['Et da Estônia', 17, 170]
var usuario2 = ['Pessoa do Pântano', 39, 198]
var usuario3 = ['Homem da Lua Virada', 21, 149]
var usuario4 = ['Pequena Paulistana', 18, 171]
var usuario5 = ['Menino da Porteira', 13, 142]


var usuario1 = ['Et da Estônia', 17, 170]
var usuario2 = ['Pessoa do Pântano', 39, 198]
var usuario3 = ['Homem da Lua Virada', 21, 149]
var usuario4 = ['Pequena Paulistana', 18, 171]
var usuario5 = ['Menino da Porteira', 13, 142]

function maiorAlto(usuario) {

    if (usuario[1] >= 18 && usuario[2] >= 170) {
        return true;

    }
    else {
        return false;
    }

}

var usuario1 = ['Et da Estônia', 17, 170]
var usuario2 = ['Pessoa do Pântano', 39, 198]
var usuario3 = ['Homem da Lua Virada', 21, 149]
var usuario4 = ['Pequena Paulistana', 18, 171]
var usuario5 = ['Menino da Porteira', 13, 142]

var resultado = maiorAlto(usuario4)

function maiorAlto(usuario) {

    if (usuario[1] >= 18 && usuario[2] >= 170) {
        return true;

    }
    else {
        return false;
    }

}

var usuario1 = ['Et da Estônia', 17, 170];
var usuario2 = ['Pessoa do Pântano', 39, 198];
var usuario3 = ['Homem da Lua Virada', 21, 149];
var usuario4 = ['Pequena Paulistana', 18, 171];
var usuario5 = ['Menino da Porteira', 13, 142];

//var resultado = maiorAlto(usuario1);

console.log("usuario1 = " +maiorAlto(usuario1));
console.log("usuario2 = " +maiorAlto(usuario2));
console.log("usuario3 = " +maiorAlto(usuario3));
console.log("usuario4 = " +maiorAlto(usuario4));
console.log("usuario5 = " +maiorAlto(usuario5));

  • I think my editing helped a little!! Your answer is great but it has repeated code snippets

  • Yes! was fundamental. Thank you!!

0

To make it easier, you can create an array of objects by passing the students' data in a single variable, and later traverse it in its function. To declare multiple users in a single variable of objects, we can do as follows:

let usuarios = [
    { nome: 'Pessoa do Pântano', idade: 39, altura: 198 },
    { nome: 'Homem da Lua Virada', idade: 21, altura: 149 },
    { nome: 'Pequena Paulistana', idade: 18, altura: 171 },
    { nome: 'Menino da Porteira', idade: 13, altura: 142 }
];

In specific to its function, you can add two conditions, verifying that the age is greater than or equal to 18 and the height greater than 170, as requested in the statement:

Be 18 years or older and of a height of 1,70 or greater.

function maiorAlto(usuario) {
    return usuario.idade >= 18 && usuario.altura >= 170;
}

With this would already solve the problem, however, in order to test, we can go through our vector calling our function (which returns true or false), allowing us to know if a particular student fits the training or not.

The final code of the solution will be as follows:

let usuarios = [
    { nome: 'Pessoa do Pântano', idade: 39, altura: 198 },
    { nome: 'Homem da Lua Virada', idade: 21, altura: 149 },
    { nome: 'Pequena Paulistana', idade: 18, altura: 171 },
    { nome: 'Menino da Porteira', idade: 13, altura: 142 }
];

function maiorAlto(usuario) {
    return usuario.idade >= 18 && usuario.altura >= 170;
}

for (let count = 0; count < usuarios.length; count++) {
    if (maiorAlto(usuarios[count])) {
        console.log(`Aluno "${usuarios[count].nome}" atende aos requisitos do treino.`)
    } else {
        console.log(`Aluno "${usuarios[count].nome}" não atende aos requisitos do treino.`)
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.