I have a looping structure that includes conditions for if, else if, and else. However, I have noticed that the else condition at the end of the loop is not being executed as expected. I am currently investigating why this might be happening. Here is the code snippet in question:
var loopEnd = 1;
while (loopEnd != 2) {
var conversion = prompt("Do you want to convert miles to km, or km to miles?");
if (conversion == "miles to km") {
var miles = 0;
var miles = prompt("How many miles do you want to convert?");
var kilometers = 1.61 * miles;
while (miles != 0) {
alert("The number of kilometers for " + miles + " miles is " + kilometers + " kilometers.");
var miles = prompt("How many miles do you want to convert?");
var kilometers = 1.61 * miles;
}
var promptEnd = prompt("Do you want to convert anything else?");
if (promptEnd == "no") {
var loopEnd = 2;
} else {
alert("Okay, let's convert something else!");
}
} else if (conversion == "km to miles") {
var kilometers = 0;
var kilometers = prompt("How many kilometers do you want to convert?");
var miles = 0.62 * kilometers;
while (kilometers != 0) {
alert("The number of miles for " + kilometers + " kilometers " + miles + " miles.");
var kilometers = prompt("How many kilometers do you want to convert?");
var miles = 1.61 * kilometers;
}
var promptEnd = prompt("Do you want to convert anything else?");
if (promptEnd == "no") {
var loopEnd = 2;
// } else {
alert("Sorry, I didn't quite catch that.");
}
}
}