How do you change a do while loop to a while loop?

Viewed 159

One of the questions on my assignment was to switch a do while into a while loop. I am a little stuck I tried moving it around a butch different ways but my code still doesn't output correctly.

do {
  userChoice = window.prompt("Press a to add a Robot Step to take\n, Press r to remove a Robot Step last Step to take\n, Press f to remove a Robot First Step to take\n, Press p to make the robot move the steps,");
  userChoice = userChoice.toLowerCase();

  switch(userChoice) {
    case "a":
      addStepsPerMovement(stepsPerMovement);
      break;
    case "r":
      removeLastMovement(stepsPerMovement);
      break;
    case userChoice == "f":
      removeFirstMovement(stepsPerMovement);
      break;
    case "p":
      printByWhileLoop (stepsPerMovement);
      break;
    default:
      document.write("Erroneous Choice\n");
      break;
  }
  userContinue = window.prompt("Do you want to continue?, y or n");
  userContinue = userContinue.toLowerCase();

} while (userContinue == "y") 
3 Answers

You can easily interchange a do-while with:

 while(true) {
   /* body */
  if(!/*condition*/)
     break;
 }

that ?

let userContinue = "y"

while (userContinue == "y") 
  {
  userChoice = window.prompt("Press a to add a Robot Step to take\n, Press r to remove a Robot Step last Step to take\n, Press f to remove a Robot First Step to take\n, Press p to make the robot move the steps,");
  userChoice = userChoice.toLowerCase();

  switch(userChoice)
    {
    case "a":
      addStepsPerMovement(stepsPerMovement);
      break;
    case "r":
      removeLastMovement(stepsPerMovement);
      break;
    case "f":
      removeFirstMovement(stepsPerMovement);
      break;
    case "p":
      printByWhileLoop (stepsPerMovement);
      break;
    default:
      document.write("Erroneous Choice\n");
      break;
    }
  userContinue = window.prompt("Do you want to continue?, y or n");
  userContinue = userContinue.toLowerCase();
  } 

Hi This is very simple , The difference between do while and while is: In do while the code block will execute one time irrespective of while condition. but in while , if and only if the condition is true then only execute. enter image description here

So according to your code here is the snippet:

Declare a function which contains the body section of do while.

function dowhiletoWhile() {
   userChoice = window.prompt("Press a to add a Robot Step to take\n, Press r to remove a Robot Step last Step to take\n, Press f to remove a Robot First Step to take\n, Press p to make the robot move the steps,");
  userChoice = userChoice.toLowerCase();

  switch(userChoi`enter code here`ce) {
    case "a":
      addStepsPerMovement(stepsPerMovement);
      break;
    case "r":
      removeLastMovement(stepsPerMovement);
      break;
    case userChoice == "f":
      removeFirstMovement(stepsPerMovement);
      break;
    case "p":
      printByWhileLoop (stepsPerMovement);
      break;
    default:
      document.write("Erroneous Choice\n");
      break;
  }
  userContinue = window.prompt("Do you want to continue?, y or n");
  userContinue = userContinue.toLowerCase();
}



dowhiletoWhile();


 while (userContinue == "y"){
 dowhiletoWhile();
}
Related