Home > Blockchain >  How to repeat an iteration
How to repeat an iteration

Time:09-30

I am trying to complete a javascript assignment for school. I should maybe add that this is not the entire assignment, this is just part of it. I have tried searching for this but could not find anything. Maybe I am not asking the right question.

I have to prompt the user as to how many people are ordering food. I then have to give each person an order option. After they choose their order I then confirm if they would like to order another meal.

As it sits right now, after the confirmation to order another meal, it just goes on to the next person instead of prompting the same customer about their meal options.

I am not sure how to repeat the iteration for the same person.

If customer 1 wants to order another meal, how do I get it to repeat so that I can add another meal to customer 1's total?

This has to be done with loops.

 let numOfPeople = Number(prompt("How many people are ordering food?", 4));

  for (let i = 1; i <= numOfPeople; i  ) {
    let mealOption = prompt(
      "Hello customer # "  
        i  
        "\n"  
        "Enter meal option (A,B,C,D)"  
        "\n"  
        "A - Hamburger and Fries: $9.95"  
        "\n"  
        "B - Soup and Salad: $13.50"  
        "\n"  
        "C - Turkey Wrap and Veggies: $7.95"  
        "\n"  
        "D - Hot Dog and Onion Rings: $11.50"
    );
    let anotherMeal = confirm("Customer "   i   ", would you like to order another meal?");
  }

CodePudding user response:

In this case the do-while loop could be good choice, because you want to choose mealOption always at least once and then, if condition is true, ask for more

for (let i = 1; i <= numOfPeople; i  ) {
  do {
    //choose mealOption
  } while(confirm(`Customer ${i}, would you like to order another meal?`)) 
}

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

  • Related