我的任务是:“本田-丰田汽车保险:
编写一个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循环,但是我就是不能让它工作(所以我把它注释掉了)。我是一个初学者,正在上在线课程,所以我正在尽我所能地利用给我的信息,但不幸的是,教授没有发布任何关于如何用数值以外的任何东西做循环的例子。除了循环部分之外,上面的代码运行得很好。请帮助我,因为我的教授评分非常慢,没有提供具体的反馈。
发布于 2020-10-20 00:45:19
你已经得到了它的大部分。尝试返回prompt的值,然后将其用作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');https://stackoverflow.com/questions/64431547
复制相似问题