Javascript - User Data Doubts

Asked

Viewed 164 times

-2

if (user[1] >= 18 && user[2] >= 170) {

This line of code below I was doubtful someone explains to me please, for me it does not make much sense to take user 1 and user 2 to check if it is true or false, I may be mistaken but it would not have to be used a user for all ?

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(["Amazing student", 18, 170]) // returns true maiortAlto(["Low student", 17, 150]) // returns 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 brackets: array[1]

Higher Function (user) {

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

}
else {
    return false;
}

}

var usuario1 = ['Et from Estonia', 17, 170]; var usuario2 = ['Swamp Person', 39, 198]; var usuario3 = ['Moon Man Turned', 21, 149]; var usuario4 = ['Pequena Paulistana', 18, 171]; var usuario5 = ['Boy Gate', 13, 142];

//var result = highest (user 1);

console.log("user 1 = " +highest (user 1)); console.log("user2 = " +mayAlto(user2)); console.log("user3 = " +mayAlto(user3)); console.log("user4 = " +mayAlto(user4)); console.log("user5 = " +highest(user5));

2 answers

0


That is correct.

This is the use of array (vector or matrix) of variables.

Note that: var usuario1 = ['Et da Estônia', 17, 170];

Is the same as:

usuario1[0] = 'Et da Estônia'; usuario1[1] = 17; usuario1[2] = 170;

They are data from the same user. Name, age and height respectively.

Then your job would be like this:

function maiorAlto(usuario){
return (usuario[1] >= 18 && usuario[2] >= 170);

}

Note that the function return is already boolean, so you can write the conditional expression already in Return, no need to check with if() to use true Return.

Example call for this function:

var usuario1 = ['Eduardo', 17, 175];
console.log(maiorAlto(usuario1));

In case of working with Multidimensional Array as:

var 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 } ];

You will have to make a loop to access each object within the array. There are two ways to do, loop for with a counter or foreach loop.

Loop for:

    function maiorAlto(usuario){
for(i = 0; i < usuario.length; i ++)
    console.log(usuario[i].idade >= 18 && usuario[i].altura >= 170);

}

maiorAlto(usuarios);

Or, using foreach:

function maiorAlto(usuario){
console.log(usuario.idade >= 18 && usuario.altura >= 170);

}

Using the call to the function in this way:

usuarios.forEach(maiorAlto);

I recommend reading these articles for further clarification: https://www.geeksforgeeks.org/multidimensional-array-in-javascript/ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

  • Look at how to make my IF if if it made a single variable for users so : var usuarios = [ { name: 'Swamp Person', age: 39, height: 198 }, { name: 'Moon Man Turned', age: 21, height: 149 }, { name: 'Little Paulistana', age: 18, height: 171 }, { name: 'Boy Doorman', age: 13, height: 142 } ]; Not a variable for each user

-2

If already returns true or false, you do not need to specify this in its body. just give a user;

  • Got it, only that the if would not be applied only in the variable 1 and 2 ? Because I have chosen only those two variables within the if. It wouldn’t have to be an if you count all users ?

  • Then in case you have to do a for, to be able to compare all users. Ideally, you create a user variable of the array type, to store all users and do a sweep through all positions of the array. Inside for for you create a variable to store the largest and then you would use an if to check if the new user is larger than the last. Then at the end you return the greatest.

Browser other questions tagged

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