首页 > 解决方案 > 不断陷入循环

问题描述

为我的介绍类和我的 html 处理一些单词问题一直卡在我的代码的警报部分中的循环中。一旦它进入那里,无论我输入是否正确,它都不会回来。这是一个初学者课程,所以我们才刚刚进入循环和开关,所以请保持基本。提前致谢!

var custOwed, numCust, dayTotal;
var woodTotal, alumTotal, steelTotal;
var rate;

var woodCount = 0;
var alumCount = 0;
var steelCount = 0;
var woodAccum = 0;
var alumAccum = 0;
var steelAccum = 0;

var anotherOrder = "yes";

var woodRate = 10;
var alumRate = 15;
var steelRate = 25;

var errorFlag = 0;

anotherOrder = prompt("Another Order", "yes or no");

while (anotherOrder == "yes") {
  poleType = prompt("What type of pole would you like today?", "wood, aluminum, or steel");
  numFeet = prompt("How many feet?", "10");
  numFeet = parseInt(numFeet);

  switch (poleType) {
    case "wood":
      woodCount = woodCount + 1;
      woodAccum = woodAccum + numFeet;
      rate = woodRate;
      break;
    case "aluminum":
      alumCount = alumCount + 1;
      alumAccum = alumAccum + numFeet;
      rate = alumRate;
      break;
    case "steel":
      steelCount = steelCount + 1;
      steelAccum = steelAccum + numFeet;
      rate = steelRate;
      break;
    default:
      errorFlag = 1;
      break;
  }
  if (errorFlag == 1) {
    alert("You typed " + poleType + " the only choices were wood, aluminum, or steel");
    anotherOrder = "yes";
  } else {
    custOwed = numFeet * rate;
    alert("You Owe" + custOwed);
    anotherOrder = prompt("Another Customer?", "yes or no");
  }
}
numCust = woodCount + alumCount + steelCount;
woodTotal = woodAccum * woodRate;
alumTotal = alumAccum * alumRate
steelTotal = steelAccum * steelRate
dayTotal = woodTotal + alumTotal + steelTotal;

document.write("Number of Customers: " + numCust);
document.write("<br>Wood Customers: " + woodCount);
document.write("<br>Total Feet of Wood: " + woodAccum);
document.write("<br>Total owed of Wood: $" + woodTotal.toFixed(2));
document.write("<br>Aluminum Customers: " + alumCount);
document.write("<br>Total Feet of Aluminum: " + alumAccum);
document.write("<br>Total owed of Aluminum: $" + alumTotal.toFixed(2));
document.write("<br>Steel Customers: " + alumCount);
document.write("<br>Total Feet of Steel: " + alumAccum);
document.write("<br>Total owed of Steel: $" + alumTotal.toFixed(2));
document.write("<br>Total owed from the day: $" + dayTotal.toFixed(2));

标签: javascripthtml

解决方案


推荐阅读