-5

function enigma(inputValue) {
  var i = 0;
  while (inputValue) {
    if (++i == 5) inputValue = 0;
  }
  return i;
}
var whatAmI = enigma(5);
console.log(whatAmI);
<div>Hit F12 and go to the console to view output.</div>

The output I get is 5.

Why does JavaScript stop the loop if inputValue equals 0? Is this because I did not specify that as the condition in the while loop?

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Mocrosoft
  • 11
  • 2

2 Answers2

0

This explains the basics of a while loop. The condition within the while loop checks if it is truthy/falsy. When i is equal to 5, the input is 0, which causes the condition to be false.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while

If you want to see some other crazy Javascript, check out these truthy/falsy examples.

http://blog.falafel.com/the-truth-about-false-in-javascript/

user201827
  • 21
  • 1
-1

0 is false. You can check here

xangy
  • 1,185
  • 1
  • 8
  • 19