Reputation: 11
This small app is to tell how fast a driver was going, when I test this code it only alerts the first condition, no matter what my entry is and this is a serious bug, this is the code I appreciate all help thanks in advance to all
alert(
"Welcome this program tells you how fast you were driving your
vehicle, in km/h");
var top_speed = parseInt(prompt("How many km/h you was at?"));
if (top_speed >= 60) {
alert("You were driving at " + top_speed + "km/h thats a normal speed");
} else if (top_speed >= 80) {
alert("You were driving at " + top_speed + "km/h thats a moderate
speed");
} else if (top_speed >= 120) {
alert("You were driving at " + top_speed + "km/h that is a very high
speed");
} else {
alert("You need to go faster " + top_speed + "km/h is too slow ");
}
Upvotes: 0
Views: 29
Reputation: 17190
You need to reverse your check conditions:
Just imagine, when you input 140
, that value is greater than 60
so the code will enter on the first if
condition, and none of the other conditions will be inspected.
alert("Welcome this program tells you how fast you were driving your vehicle, in km/h");
var top_speed = parseInt(prompt("How many km/h you was at?"));
if (top_speed >= 120)
{
alert("You were driving at " + top_speed + "km/h thats a very high speed");
}
else if (top_speed >= 80)
{
alert("You were driving at " + top_speed + "km/h thats a moderate speed");
}
else if (top_speed >= 60)
{
alert("You were driving at " + top_speed + "km/h that is a normal speed");
}
else
{
alert("You need to go faster " + top_speed + "km/h is too slow ");
}
Upvotes: 0
Reputation:
In addition to the good answers already here (they are the true answer), I think I should also mention this in case someone else has a similar problem.
Else if is only executed if the first condition is false. That is to say, a string of else ifs will ONLY execute the FIRST true condition. If none exists, only then will it execute the else or, if none exists, do nothing.
Upvotes: 0
Reputation: 36564
This is because the first condition top_speed >= 60
is true
whenever the other two conditions top_speed >= 80
and top_speed >= 120
are true.Whenever any of conditions will be true
it will not check others which are below that. You should change the order.
var top_speed = parseInt(prompt("How many km/h you was at?"));
if (top_speed >= 120) {
alert("You were driving at " + top_speed + "km/h that is a very high speed");
}
else if (top_speed >= 80) {
alert("You were driving at " + top_speed + "km/h thats a moderate speed");
}
else if (top_speed >= 60) {
alert("You were driving at " + top_speed + "km/h thats a normal speed");
}
else {
alert("You need to go faster " + top_speed + "km/h is too slow ");
}
Upvotes: 0
Reputation: 14255
The statements are evaluated from top to bottom. As 80 and 120 are both also >= 60
, the first condition will always match for values of 60 and above.
Simply switch the order:
if (top_speed >= 120) {
alert("You were driving at " + top_speed + "km/h that is a very high
speed");
} else if (top_speed >= 80) {
alert("You were driving at " + top_speed + "km/h thats a moderate
speed");
} else if (top_speed >= 60) {
alert("You were driving at " + top_speed + "km/h thats a normal speed");
} else {
alert("You need to go faster " + top_speed + "km/h is too slow ");
}
An alternative would be to be more specific in your conditions, i.e. also include an upper bound:
if (top_speed >= 60 && top_speed < 80) {
alert("You were driving at " + top_speed + "km/h thats a normal speed");
} else if (top_speed >= 80 && top_speed < 120) {
alert("You were driving at " + top_speed + "km/h thats a moderate
speed");
} else if (top_speed >= 120) {
alert("You were driving at " + top_speed + "km/h that is a very high
speed");
} else {
alert("You need to go faster " + top_speed + "km/h is too slow ");
}
Upvotes: 3