Function with three parameters in Javascript. Error returning "true" or "false". What to do?

Asked

Viewed 59 times

-4

I need a code that signals TRUE when the age is >= 21 years and the height >= 180 cm. The code I made is not working and I don’t know which error shows. =/

function maiorAlto(nome , idade , altura) {
    if (idade >=21 && altura >= 181) {
        return true 
    } else {
        return false
    }
}
 
 maiorAlto(['Laura', 21, 187])

1 answer

-1


You are incorrectly passing the parameters in the function call.

The right thing would be:

maiorAlto('Laura', 21, 187);

This code is also missing semicolon to indicate the end of the instructions.

Now, if you want to pass an array as a parameter, then the function is misspelled. It should be:

function maiorAlto(pessoa) {
    if (pessoa[1] >=21 && pessoa[2] >= 181) {
        return true; 
    } else {
        return false;
    }
}

Note that since the expected return is boolean (true or false) you do not need to use if. You could do so:

function maiorAlto(pessoa) {
        return (pessoa[1] >=21 && pessoa[2] >= 181);
}

Browser other questions tagged

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