while(userInput!= -1)
{
var total=0.0;
var totalEarnings=0;
var item;
//var userInput;
var salesPerson=1;
var userInput= parseInt(prompt("outsidePlease enter noOfItemsSold of Item# 1 sold for SalesPerson #"+salesPerson+"\n"+" OR -1 to Exit "));
salesPerson++;
//alert("in while "+salesPerson);
//userInput= parseInt(prompt("Please enter noOfItemsSold of Item# "+item+" sold for SalesPerson #"+salesPerson+"\n"+" OR -1 to Exit "));
//if(userInput!=-1)
//{
for(item=2;item<5;item++)
{
//userInput= parseInt(prompt("Please enter noOfItemsSold of Item# "+item+" sold for SalesPerson #"+salesPerson+"\n"+" OR -1 to Exit "));
//var noOfItemsSold = parseInt(userInput);
if (item == 1)
{
total += userInput * 239.99;
}
else if (item == 2)
{
total += userInput * 129.75;
}
else if (item == 3)
{
total += userInput * 99.95;
}
else if (item == 4)
{
total += userInput * 350.89;
}
var input= parseInt(prompt("Please enter noOfItemsSold of Item# "+item+" sold for SalesPerson #"+salesPerson+"\n"+" OR -1 to Exit "));
}
totalEarnings = .09 * total + 200;
document.writeln("<h1>SalesPerson#"+salesPerson+" Total Earnings this week is " +totalEarnings +"</h1>");
}
我試圖運行連續循環。腳本正在計算銷售人員的總收入,我想運行腳本直到用戶輸入-1。 我不知道我在做什麼錯,但這是行不通的。 我該如何解決這個問題?如何在javascript中運行連續循環
我想你可能需要在while循環之外實例化userInput,而不是在它之內。 – Bob
當你說*這不工作*,你能更具體嗎? –
@Bob - 我認爲可變提升實際上會讓這個工作,但我同意它馬虎。 –