Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to repeat an iteration

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?");
  }

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading