Infinite loop issue


#1


I am having issues with the following code that seems to enter an infinite loop. Basically I want the code to end if the input for moreCalc is not y or Y after the initial run, yet it continues infinitely.

Any help would be greatly appreciated.

do {
  var op = prompt("Type a number from 1-4");


  var firstNumber = prompt("Type a number");
  var secondNumber = prompt("Type a number");
  var result = 1;


  if (op == 3)
  {
    result = firstNumber * secondNumber;
  }
  else if (op == 1)
  {
    result = firstNumber + secondNumber;
  }
  else if (op == 2)
  {
    result = firstNumber - secondNumber;
  }
  else if (op == 4)
  {
    result = firstNumber / secondNumber;
  }

  console.log("The result is" + " " + result);
  var moreCalc = prompt("Do you want to enter another calculation?");
 
}
while (moreCalc == "y" || "Y");


#2

This will always be true. Think of the two operands independently:

moreCalc == 'y'    // could be true or false

"Y"                // always true

Whether F || T or T || T the outcome will always be true.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.