dingerina
dingerina

Reputation: 57

How do I get a loop to work in this JavaScript assignment?

I had an assignment asking for the following: "The Honda-Toyota Automobile Insurance:

Writes a JavaScript Program that prints the insurance fee to pay for a Vehicle according to the following rules:

 A Honda that is All Wheel Drive costs $450.

 A Honda that is two wheels’ drive costs $350.

 A Toyota that is All Wheel Drive costs $300.

 A Toyota that is Two Wheel Drive costs $250.

Any other type of vehicle generates an error message. The web page should prompt the user for the appropriate information: Asking about the car Model (Honda or Toyota) and then if it’s an All-wheel drive or not, then display the insurance fee. After printing the insurance fee, the program should ask the user if he/she wants to insure an-other Vehicle using repetition structure "

I haven't had any prior experience using loops and could not figure out how to get the program to loop back to the beginning once the user answered "yes" on the prompt "Do you want to insure another vehicle?" Here is the code that I used:

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. 

The function was linked to a button on the html page and perhaps this was my issue, but I did not have time to edit all of the code I had just built in time for the deadline. I am pretty certain I need to use a do while loop, but I simply could not get it to work (hence why I commented it out). I am a beginner and taking online classes, so I am doing the best I can with the info given to me, but unfortunately the professor did not post any examples of how to do loops with anything outside of numeric values. The code above works fine aside from the loop portion. Please help because my professor is very slow on grading and does not provide specific feedback.

Upvotes: 1

Views: 97

Answers (1)

Rudraprasad Das
Rudraprasad Das

Reputation: 368

You have gotten majority of it. Try to return the value of prompt and then use it as a flag in while loop. Like this -

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');

Upvotes: 1

Related Questions