Problem Solving in Javascript

Asked

Viewed 231 times

1

I’m new to javascript, I’m doing some fixation exercises, but I’m having trouble in the case below.

Create a function called highAlto that returns true (true) if it meets the requirements(Be 18 years old or older and have a height equal to or greater than 1.70), and false (false) to the contrary. In this function the parameters that will be an array, containing in the first position the student’s age and in the second containing the height in Centimeters.

function maiorAlto(usuario){
   if (maiorAlto.length){
      if (maiorAlto.length){
         return usuario5[1] <= 18 || usuario5[2] <=170
      }
      else{
        return usuario5[1] >= 18 || usuario5[2] >=170
      }

   }
}

3 answers

2

You receive input into this function an array that is done in this mode:

[idade, altura]

You can do something simple like:

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

You make it even simpler by applying an Arrow Function:

const maiorAlto = (usuario) => (usuario[0] >= 18 && usuario[1] >= 170);

Explanation of return:

usuario[0] contains the age of the user passed as parameter, when confronting it with >= 18 this will return true if the user is over 18 years old, otherwise false, then we add a && (AND) to also check the height with usuario[1] >= 170, in words it would be something like:

True: SE age greater than or equal to 18 E height greater than or equal to 170.

If one of the statements is not either both true/true, the function will return false.

1

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").

0


@Leo-Letto and @hkotsuboi have said it all, I just came here to put another code option ...

<script>
function maiorAlto(usuario){
  
   if (usuario[1] >= 18 && usuario[2] >=170){
        alert("True");
        return true;
    
   }
   else{
        alert("False");
        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)
</script>

A more traditional way of doing it (as you said it is new in js), you can run the code above here, all who answered this question solved the problem, everything depends on which lexical level you prefer to work, I do not know if there is a difference in performance (execution) between the three options given, but you can notice how has a cleaner syntax the other options given ...

  • 1

    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 a if 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.

  • 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.

  • Responding to Leo, in solving this problem specifically they ask to use the IF.

  • 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 ....

  • @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 ...

Browser other questions tagged

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