0

Was having some fun with prompt, parseInt and do...while and just stumbled upon the following:

do {
    let length = prompt('Enter a number between 8 and 128 (inclusive) for your length.');
    length = parseInt(length, 10);
} while (length < 8 || length > 128);

Every time I would use the Chrome Developer tools to inspect the value of length during the while execution, I would see a value of 0. I thought there was something up with the radix I had chosen for parseInt but that was not the case.

I noticed that VS Code was complaining that the let length was not being used anywhere. That was odd. Once I removed the variables outside of the loop and declared as separate string and number fields instead, (i.e., let lengthPrompt and let length), everything worked out:

let lengthPrompt;
let length;

do {
  lengthPrompt = prompt('Length?');
  length = parseInt(lengthPrompt, 10);
} while (length < 8 || length > 128);

Is this an issue with scope or some other weird JS specific thing I am unaware of.

User 5842
  • 2,849
  • 7
  • 33
  • 51

0 Answers0