问题描述
我有一个作业要求以下内容: “本田-丰田汽车保险:
编写一个JavaScript程序,该程序根据以下规则打印保险费来支付车辆的费用:
本田全轮驱动汽车售价为450美元。
两轮驱动的本田汽车售价$ 350。
一辆全轮驱动的丰田汽车售价300美元。
两轮驱动的丰田汽车售价250美元。
任何其他类型的车辆都会生成错误消息。该网页应提示用户适当的信息:询问汽车型号(本田或丰田),然后询问是否为全轮驱动,然后显示保险费。 在打印了保险费之后,程序应询问用户是否要使用重复结构“
我以前没有使用过循环的经验,也无法弄清楚一旦用户在提示“您想给另一辆车保险吗”时回答“是”,如何使程序循环回到起点。这是我使用的代码:
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循环,但是我根本无法使它正常工作(因此我将其注释掉了)。我是一个初学者,正在参加在线课程,所以我正在尽我所能提供给我的信息,但是不幸的是,这位教授没有发布任何关于如何对数值以外的任何东西进行循环的例子。上面的代码除了循环部分之外还可以正常工作。请帮忙,因为我的教授评分很慢并且没有提供具体反馈。
解决方法
您已经获得了大多数。尝试返回提示符的值,然后将其用作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');