Arrays begin at zero (the first index is zero, the second is 1, etc).
In addition, to check whether two conditions are true, the operator &&
(and) and not the ||
(or).
That said, your job would be just that:
function maiorAlto(usuario) {
return usuario[0] >= 18 && usuario[1] >= 170;
}
console.log(maiorAlto([18, 170])); // true
console.log(maiorAlto([25, 210])); // true
console.log(maiorAlto([17, 180])); // false
console.log(maiorAlto([19, 150])); // false
That is, if the age is 18 or older and height is greater than or equal to 170, the whole expression results in true
. If either of the conditions is not true (or the age is less than 18, or the height is less than 170, or both), the return is false
.
It makes no sense to have the if (maiorAlto.length)
(and having it twice in a row makes even less sense). The attribute length
of a function indicates the amount of parameters she expects to receive, there’s no reason to use it here.
And within the function you use usuario5
, and the parameter is called usuario
, then it would never work properly.
What should perhaps be done is to check the array usuario
has even 2 elements and they are numbers but does not seem to be requirement of the exercise.
I just disagree with another answer as to the fact of Arrow Function be "simpler". This is relative: for me, depending on the case, it gets even more confusing (and in the case of a simple function like this, I find even an unnecessary complication, without any gain in fact). It is also worth remembering that a Arrow Function not always 100% equivalent to a declared function with function
, see more about this here, here and here (it is not the case of the above function, but it is important to know the differences instead of using just because it seems "simpler" or "cooler").
In this specific case it has virtually no difference in performance, but in case it is not necessary to treat any data to the internal of the function, simply control the parameters passed, then once done the
AND
with both values, it is not necessary to do another check with aif
since the value controlled before is already the end result to be returned, you are doing another control over a value that has been controlled basically.– Leo Letto
In case the result of the emulator I am using gives the following answer -> Errors: The higher functionAlto must return true when executed with the information: highest(["Et de Estonia", 18, 170]) The highest function should return false when executed with the information: highest(['Moon Man Turned', 21, 149]), until the last one is user5, and the part of Function down was I who created them they only passed me the parameters of the users.
– BRUNO MARTINS FERNANDES
Responding to Leo, in solving this problem specifically they ask to use the IF.
– BRUNO MARTINS FERNANDES
In theory this is what I also imagine, but the processor will have to do a logical operation anyway, one day we could test if it has any performance gain, it would be cool, I have enough interest to know if in the end makes a difference in running, in college I remember that my teachers made me do these performance tests, but I never did anything like this with JS rsrs ....
– ederwander
@BRUNOMARTINSFERNANDES boy was what I imagined hahaha is something for those who are starting, so they will want an exercise with IF rsrs, apparently my code should meet what you need ...
– ederwander