Why "negative if" number don't counting in right way? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

Why "negative if" number don't counting in right way?

When let adults=-1; let children=2; let price = adults*12 + children*6; if(adults < 0) { adults = 0; } if(children < 0) { children = 0; } console.log(price); The console show me — 0 But if adults is negative it's equal 0, then price=0*12+2*6=12 So, console need to show — 12 What's wrong?

17th Jan 2023, 7:23 PM
Iryna Y
Iryna Y - avatar
1 Réponse
+ 4
Your price variable is calculated only once, at the beginning. Even if you change the other variables (adults and children) later in the program, those will not affect the calculation any more. Move the "let price" line after your if conditions.
17th Jan 2023, 7:29 PM
Tibor Santa
Tibor Santa - avatar