如何让循环在此 JavaScript 作业中工作?
How do I get a loop to work in this JavaScript assignment?
我有一个作业要求以下内容:
“本田-丰田汽车保险:
编写一个 JavaScript 程序,根据以下规则打印车辆保险费:
全轮驱动的本田售价 450 美元。
两轮驱动的本田售价 350 美元。
全轮驱动的丰田售价 300 美元。
两轮驱动的丰田售价 250 美元。
任何其他类型的车辆都会生成错误消息。该网页应提示用户提供适当的信息:询问汽车型号(本田或丰田),然后询问是否是全轮驱动,然后显示保险费。
打印保险费后,程序应询问用户是否 he/she 想要使用重复结构“
为另一辆车投保
我以前没有任何使用循环的经验,并且无法弄清楚一旦用户在提示“你想为另一辆车投保吗?”时回答“是”,如何让程序循环回到开头。 “这是我使用的代码:
function feeCalc() //Linked to button on html page. Perhaps this was the issue?
{
let carType = prompt('Do you have a Honda or Toyota?');
//changes case of input string to lowercase
let carTypeLC = carType.toLowerCase();
//prices of different insurance
let hondaAWD = "450.00";
let honda2WD = "350.00";
let toyotaAWD = "300.00";
let toyota2WD = "250.00";
if ((carTypeLC == 'honda') || (carTypeLC == 'toyota')) {
let wheelDrive = prompt('Does your vehicle have All Wheel Drive or Two Wheel Drive? Enter \"AWD\" or \"2WD\".');
//changes case of input string to lowercase
let wheelDriveLC = wheelDrive.toLowerCase();
if ((carTypeLC == 'honda') && (wheelDriveLC == 'awd')) {
alert('The insurance fee for your Honda AWD is $' + hondaAWD);
document.getElementById("webDisplay").innerHTML = 'The insurance fee for your Honda AWD is $' + hondaAWD;
} else if ((carTypeLC == 'honda') && (wheelDriveLC == '2wd')) {
alert('The insurance fee for your Honda 2WD is $' + honda2WD);
document.getElementById("webDisplay").innerHTML = 'The insurance fee for your Honda 2WD is $' + honda2WD;
} else if ((carType = 'toyota') && (wheelDrive == 'awd')) {
alert('The insurance fee for your Toyota AWD is $' + toyotaAWD);
document.getElementById("webDisplay").innerHTML = 'The insurance fee for your Toyota AWD is $' + toyotaAWD;
} else if ((carType = 'toyota') && (wheelDrive == '2wd')) {
alert('The insurance fee for your Toyota 2WD is $' + toyota2WD);
document.getElementById("webDisplay").innerHTML = 'The insurance fee for your Toyota 2WD is $' + toyota2WD;
} else {
alert('There seems to be an error. Please try again.');
}
} else {
alert('There seems to be an error. Please try again.');
}
let restart = prompt('Do you want to insure another vehicle? \"y\" or \"n\"')
}
do {
feeCalc
} while (restart = 'y' && restart != 'n');
//I commented the above do while loop code out because it was not working and made the rest of my code not function. I had to submit the assignment so I submitted what was working.
该功能链接到 html 页面上的一个按钮,也许这是我的问题,但我没有时间在截止日期前编辑我刚刚构建的所有代码。我很确定我需要使用 do while 循环,但我就是无法让它工作(因此我将其注释掉)。我是初学者,正在上网 类,所以我会尽我所能地利用提供给我的信息,但不幸的是,教授没有 post 任何关于如何使用数值。除了循环部分,上面的代码工作正常。请帮忙,因为我的教授评分很慢,而且没有提供具体的反馈。
你已经得到了大部分。尝试 return 提示的值,然后将其用作 while 循环中的标志。像这样-
function feeCalc() //Linked to button on html page. Perhaps this was the issue?
{
let carType = prompt('Do you have a Honda or Toyota?');
//changes case of input string to lowercase
let carTypeLC = carType.toLowerCase();
//prices of different insurance
let hondaAWD = "450.00";
let honda2WD = "350.00";
let toyotaAWD = "300.00";
let toyota2WD = "250.00";
if ((carTypeLC == 'honda') || (carTypeLC == 'toyota'))
{
let wheelDrive = prompt('Does your vehicle have All Wheel Drive or Two Wheel Drive? Enter \"AWD\" or \"2WD\".');
//changes case of input string to lowercase
let wheelDriveLC = wheelDrive.toLowerCase();
if ((carTypeLC =='honda') && (wheelDriveLC =='awd'))
{
alert('The insurance fee for your Honda AWD is $' + hondaAWD);
document.getElementById("webDisplay").innerHTML = 'The insurance fee for your Honda AWD is $' + hondaAWD;
}
else if ((carTypeLC == 'honda') && (wheelDriveLC =='2wd'))
{
alert('The insurance fee for your Honda 2WD is $' + honda2WD);
document.getElementById("webDisplay").innerHTML = 'The insurance fee for your Honda 2WD is $' + honda2WD;
}
else if ((carType=='toyota') && (wheelDrive=='awd'))
{
alert('The insurance fee for your Toyota AWD is $' + toyotaAWD);
document.getElementById("webDisplay").innerHTML = 'The insurance fee for your Toyota AWD is $' + toyotaAWD;
}
else if ((carType=='toyota') && (wheelDrive=='2wd'))
{
alert('The insurance fee for your Toyota 2WD is $' + toyota2WD);
document.getElementById("webDisplay").innerHTML = 'The insurance fee for your Toyota 2WD is $' + toyota2WD;
}
else
{
alert('There seems to be an error. Please try again.');
}
}
else {
alert('There seems to be an error. Please try again.');
}
return prompt('Do you want to insure another vehicle? \"y\" or \"n\"')
}
// use the variable restart as a flag to break off the do...while loop
let restart = 'y';
do
{
restart = feeCalc();
}
while (restart === 'y');
我有一个作业要求以下内容: “本田-丰田汽车保险:
编写一个 JavaScript 程序,根据以下规则打印车辆保险费:
全轮驱动的本田售价 450 美元。
两轮驱动的本田售价 350 美元。
全轮驱动的丰田售价 300 美元。
两轮驱动的丰田售价 250 美元。
任何其他类型的车辆都会生成错误消息。该网页应提示用户提供适当的信息:询问汽车型号(本田或丰田),然后询问是否是全轮驱动,然后显示保险费。 打印保险费后,程序应询问用户是否 he/she 想要使用重复结构“
为另一辆车投保我以前没有任何使用循环的经验,并且无法弄清楚一旦用户在提示“你想为另一辆车投保吗?”时回答“是”,如何让程序循环回到开头。 “这是我使用的代码:
function feeCalc() //Linked to button on html page. Perhaps this was the issue?
{
let carType = prompt('Do you have a Honda or Toyota?');
//changes case of input string to lowercase
let carTypeLC = carType.toLowerCase();
//prices of different insurance
let hondaAWD = "450.00";
let honda2WD = "350.00";
let toyotaAWD = "300.00";
let toyota2WD = "250.00";
if ((carTypeLC == 'honda') || (carTypeLC == 'toyota')) {
let wheelDrive = prompt('Does your vehicle have All Wheel Drive or Two Wheel Drive? Enter \"AWD\" or \"2WD\".');
//changes case of input string to lowercase
let wheelDriveLC = wheelDrive.toLowerCase();
if ((carTypeLC == 'honda') && (wheelDriveLC == 'awd')) {
alert('The insurance fee for your Honda AWD is $' + hondaAWD);
document.getElementById("webDisplay").innerHTML = 'The insurance fee for your Honda AWD is $' + hondaAWD;
} else if ((carTypeLC == 'honda') && (wheelDriveLC == '2wd')) {
alert('The insurance fee for your Honda 2WD is $' + honda2WD);
document.getElementById("webDisplay").innerHTML = 'The insurance fee for your Honda 2WD is $' + honda2WD;
} else if ((carType = 'toyota') && (wheelDrive == 'awd')) {
alert('The insurance fee for your Toyota AWD is $' + toyotaAWD);
document.getElementById("webDisplay").innerHTML = 'The insurance fee for your Toyota AWD is $' + toyotaAWD;
} else if ((carType = 'toyota') && (wheelDrive == '2wd')) {
alert('The insurance fee for your Toyota 2WD is $' + toyota2WD);
document.getElementById("webDisplay").innerHTML = 'The insurance fee for your Toyota 2WD is $' + toyota2WD;
} else {
alert('There seems to be an error. Please try again.');
}
} else {
alert('There seems to be an error. Please try again.');
}
let restart = prompt('Do you want to insure another vehicle? \"y\" or \"n\"')
}
do {
feeCalc
} while (restart = 'y' && restart != 'n');
//I commented the above do while loop code out because it was not working and made the rest of my code not function. I had to submit the assignment so I submitted what was working.
该功能链接到 html 页面上的一个按钮,也许这是我的问题,但我没有时间在截止日期前编辑我刚刚构建的所有代码。我很确定我需要使用 do while 循环,但我就是无法让它工作(因此我将其注释掉)。我是初学者,正在上网 类,所以我会尽我所能地利用提供给我的信息,但不幸的是,教授没有 post 任何关于如何使用数值。除了循环部分,上面的代码工作正常。请帮忙,因为我的教授评分很慢,而且没有提供具体的反馈。
你已经得到了大部分。尝试 return 提示的值,然后将其用作 while 循环中的标志。像这样-
function feeCalc() //Linked to button on html page. Perhaps this was the issue?
{
let carType = prompt('Do you have a Honda or Toyota?');
//changes case of input string to lowercase
let carTypeLC = carType.toLowerCase();
//prices of different insurance
let hondaAWD = "450.00";
let honda2WD = "350.00";
let toyotaAWD = "300.00";
let toyota2WD = "250.00";
if ((carTypeLC == 'honda') || (carTypeLC == 'toyota'))
{
let wheelDrive = prompt('Does your vehicle have All Wheel Drive or Two Wheel Drive? Enter \"AWD\" or \"2WD\".');
//changes case of input string to lowercase
let wheelDriveLC = wheelDrive.toLowerCase();
if ((carTypeLC =='honda') && (wheelDriveLC =='awd'))
{
alert('The insurance fee for your Honda AWD is $' + hondaAWD);
document.getElementById("webDisplay").innerHTML = 'The insurance fee for your Honda AWD is $' + hondaAWD;
}
else if ((carTypeLC == 'honda') && (wheelDriveLC =='2wd'))
{
alert('The insurance fee for your Honda 2WD is $' + honda2WD);
document.getElementById("webDisplay").innerHTML = 'The insurance fee for your Honda 2WD is $' + honda2WD;
}
else if ((carType=='toyota') && (wheelDrive=='awd'))
{
alert('The insurance fee for your Toyota AWD is $' + toyotaAWD);
document.getElementById("webDisplay").innerHTML = 'The insurance fee for your Toyota AWD is $' + toyotaAWD;
}
else if ((carType=='toyota') && (wheelDrive=='2wd'))
{
alert('The insurance fee for your Toyota 2WD is $' + toyota2WD);
document.getElementById("webDisplay").innerHTML = 'The insurance fee for your Toyota 2WD is $' + toyota2WD;
}
else
{
alert('There seems to be an error. Please try again.');
}
}
else {
alert('There seems to be an error. Please try again.');
}
return prompt('Do you want to insure another vehicle? \"y\" or \"n\"')
}
// use the variable restart as a flag to break off the do...while loop
let restart = 'y';
do
{
restart = feeCalc();
}
while (restart === 'y');