Tiffany is writing a program to help manage a bake sale. She writes the following code which prompts the user to enter the total number of items a person is buying and then a loop repeatedly prompts for the cost of each item. She wrote the code but there is a problem: it runs in an infinite loop. How can Tiffany change her code so it doesn't loop forever?line 0 -- var numItems = promptNum("How many items?");line 1-- var total = 0;line 2-- while (numItems > 0){line 3-- total = total + promptNum("Enter next item price");line4-- }line 5-- console.log("The total is" + total);A) Change Line 1 to:var total = -1;B) Change Line 2 to:while (itemsInCart >= 0){C) Change Line 3 to:total = promptNum("Enter next item price");D) Add after line 3:numItems = numItems - 1;E) Add after line 4:numItems = 0;

Respuesta :

Answer:

Option D is the correct answer for the above question.

Explanation:

  • The above question code executes the infinite while loop because there is no operation in the loop body which helps the loop condition to be false in any iteration of the while loop.
  • Every while loop contains the three thinks, one is starting value for the loop, the second is a condition which states that the body of the loop will execute or not and the third is an operation which states the condition variable to be false or true.
  • The above program has only two think the starting value which is given by the user in numItems variables and the condition check which decides that the value entered should be greater than 0.
  • So to states, the condition will false there needs some increment operation which states the condition variable will be false. So there must be an operation for "numItems" variables because it participates in the condition of the while loop.
  • The operation relates to the "numItems" variable is given only in option D. Hence D is the correct option while the other is not because others do not hold the operation for "numItems" variables.
ACCESS MORE
ACCESS MORE
ACCESS MORE
ACCESS MORE