function that decides whether it can climb on a JAVASCRIPT toy

Asked

Viewed 940 times

-1

BS: I’m using a learning platform, I’m trying to decipher what’s wrong. I’ll leave the question below and my code. In the city amusement park, they installed a new roller coaster and asked us for help so that we could notify people, whether they could get in or not, before queuing up. The requirements for a person to be able to enter the toy are: edit1: the system itself provides the variables.

Reach a height of at least 1,5 m (or 1,2 m if accompanied by an adult) Not having any heart problem Set the podeSubir function, receiving 3 parameters: highPessoa (number), timeCompania (boolean), temProblemaCardiaco (boolean), return true or false as appropriate. Take into account the necessary conditions mentioned above.

codeline:

function podeSubir(alturaPessoa, vemComCompania, temProblemaCardiaco){
  return alturaPessoa >= 1.5 && temProblemaCardiaco == !temProblemaCardiaco || alturaPessoa >= 1.2 && vemComCompania == vemComCompania && temProblemaCardiaco == !temProblemaCardiaco ;
}

this code does not supply some tests, but even I hammering still will not. if you can help...

  • Can you explain to us: temProblemaCardiaco == !temProblemaCardiaco?

  • as the system itself provides the values, so I only used the parameter to buy itself as a form of negation

  • 2

    And when a value is equal to itself denied?

  • good question, on second thought, but this was just an attempt before several, I used without the exclamation but sainda will not

  • 2

    Okay, so I understand that you’re trying to write code without understanding what the code does, which is a serious mistake, so I recommend you do the table test of your solution and see exactly what it does.

  • Remember that you can use the karnaugh map to build your logical expression.

  • thanks for the guidance!!!

Show 2 more comments

1 answer

3

Conditions to be true: NÃO temProblemaCardiaco E (altura >= 1,5) OU (altura >= 1,2 E vemComCompanhia), now just translate this to javascript:

function podeSubir(alturaPessoa, vemComCompania, temProblemaCardiaco){
  return !temProblemaCardiaco && (alturaPessoa >= 1.5) || (alturaPessoa >= 1.2 && vemComCompania);
}

Always remember to use () to set the execution priority. And at the beginning, read the problem, write or draw what has to be done and then implement in the language.

And another thing you compare a variable to its negation, example temProblemaCardiaco === !temProblemaCardiaco, will always be false, because it always is true you compare with false, what generates false, and when it is false, you compare with true, what again generates false. In this case, just know if the variable is true or not.

  • 1

    There’s a short-circuit problem in your recurring logic expression true when alturaPessoa is greater than 1.5 without assessing if you have a heart problem.

  • You’re right, I’ll fix it, thanks!

  • Perfect. It is even legal to bring the condition of heart problem to the beginning, as it is mandatory and does not require assessing the age of the person if it has heart problem.

  • Yeah, I always try to remember curto-circuito, but sometimes it escapes haha. Thanks!

  • I feel a little frustrated to see that the answer seemed very simple, but I imagine like anything, it takes practice. but obg for the help guys, if you can leave a suggestion how to study better, I will be grateful!

  • It happens in the beginning, then it gets picked up with practice. And as @Woss quoted, after a search on avaliações de curto-circuito, is good practice and makes the code more optimized.

Show 1 more comment

Browser other questions tagged

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