I just started learning JS, before this (as now) C # codeu. For me, JS implicit typing is just awful! There was a specific case, the code does not work successfully.

if (step + direction < 0) { document.writeln("outdown"); } else { step += direction; document.writeln(step); } 

At the same time step at the beginning of execution 0 , and direction -1 . Instead of the inscription outdown displays " 0-1 ". I understand that this is due to the fact that one variable is not an int, but a string. How to fix it, I do not know.

  • 2
    If step=0 and direction=-1 , then the output is exactly outdown . You obviously do not say something. For example, the fact that step and direction not really numbers, but strings - andreymal

1 answer 1

An example that I have compiled according to your description is working correctly.

 let step = 0; let direction = -1; if (step + direction < 0) { document.writeln("outdown"); } else { step += direction; document.writeln(step); } 

This one displays the text you specified:

 let step = "0"; let direction = -1; if (step + direction < 0) { document.writeln("outdown"); } else { step += direction; document.writeln(step); } 

The thing is that in the second example, which means that your step not 0 but "0" , i.e. line;

This can be solved either by implicitly casting to Number:

 step = +step; 

or by explicitly calling the Number function:

 step = Number(step); 
  • I had step = 0; but + step solved the problem. Thank! - Andrew