mardi 31 octobre 2017

while loop terminating early with nested if statement

I'm writing a condition check for a simple input via prompt() in JavaScript. The idea is to repeat a prompt until the user supplies a positive integer.

In particular, if I submit a negative integer, it correctly prompts me again via case 3. However, if I answer the second prompt again with a negative integer, the code unexpectedly leaves the while loop.

In the if statement:

Case 1: checks if the user 'cancels' the prompt box (or returns an empty string), in which case I set a variable 'cancelGridCreation' to 'true' to later cancel the encapsulating process (outside of the included code), and then terminate the while loop by setting the variable 'waitingForQualifyingAnswer' to 'false'.

Case 2: checks if the response is an acceptable integer (number, integer, positive), and sets the conditional variable 'false' to terminate the while loop.

Case 3: prompts the user again, with the idea that the newly entered data will be checked again by the same 3 'if' cases.

User inputs matching cases 1 and 2 seem to work fine. However, as mentioned above, entering a negative integer 2 times terminates the while loop. See the console output that I received, entering -10 and then -20. As well, the console.log() outputs don't seem to occur in an order matching their location in the code (perhaps a processing time-lag issue?). You'll see that the console.log() from case 3 outputs before the "initial size entered" console.log(), which is coded before the while loop even starts.

Similar results seem to occur whenever case 3 is followed by another case 3 (regardless whether input is negative integers, decimals, or strings), as well as case 3 followed by case 1 (of course, here case 1 would terminate the loop, but the console log statements still seem to occur out of order).

Link to interactive code at JSBin: http://ift.tt/2xJ0e4r

JavaScript Code:

let cancelGridCreation = false;
let waitingForQualifyingAnswer = true;
let answer = prompt('Please enter the number of rows/columns, as a positive integer:', '16');
let gridSize = parseFloat(answer, 10);
console.log('initial size entered: ' + gridSize);
while (waitingForQualifyingAnswer === true) {
  console.log('top of while loop');
  if (answer == null || answer == "") {
    console.log('prompt - canceled');     
    cancelGridCreation = true;
    waitingForQualifyingAnswer = false;
  } else if (typeof gridSize === 'number' && gridSize % 1 === 0 && gridSize > 0) {
    console.log('prompt - good answer: ' + gridSize);
    waitingForQualifyingAnswer = false;
  } else {
    console.log('prompt - BAD answer: ' + gridSize);
    answer = prompt('Incorrect format entered.  Please enter the number of rows/columns, as a positive integer:', '16');
    gridSize = parseFloat(answer, 10);
    console.log('new size entered: ' + gridSize);
  }
  console.log('end of while loop');
}
console.log('done:');
console.log(gridSize);

Console Output was as follows (having entered -10 at the first prompt, and -20 at the second prompt):

    "prompt - BAD answer: -10"
    "top of while loop"
    "initial size entered: -10"
    "new size entered: -20"
    "end of while loop"
    "done:"
    -20

I'm pretty new, and realize there may be more concise ways to accomplish this. I'm interested in suggestions, however, I"m also keen to figure out why the while loop is ending when only case 3 inputs are being submitted.

Please note: The console.log() statements are only of a simple means of debugging. I realize a parseInt() would truncate any decimals, but I chose to allow integers as an acceptable size.

Thank you for any advice you can offer :)

Aucun commentaire:

Enregistrer un commentaire