I'm very new to JavaScript and was asked to complete a nested if question. I found that I'm having trouble executing the second part. When you choose 'B', it's all good but when you choose 'G', you only get to type in a variable for the first statement and then it justs ends there.
Also, is it possible to write multiple nested if statements? As in let's say in the code below, after I told them "Work sucks...", can I continue to prompt the user to enter a variable?
Thank you!
<!DOCTYPE>
<html>
<head>
<title>Hello</title>
<script type="text/javascript">
var ans;
var b1;
var g1;
ans = prompt ("How are you feeling?\n'B' for Bad | 'G' for Good");
if (ans == 'B')
{
b1 = prompt ("I'm sorry ... 'W'.\nIf not, type 'E'");
if (b1 == 'W')
{
window.alert ("Work sucks ....");
}
else if (b1 == 'E')
{
window.alert ("Here's ..");
}
}
else if (ans == 'G')
{
g1 == prompt("That's great! I hope you have a fantastic day ahead!\n'C' to Continue | 'R' to Exit");
if (g1 == 'C')
{
window.alert ("...");
}
else if (g1 == 'R')
{
window.alert ("Goodbye. Have a lovely day!");
}
}
</script>
</head>
<body>
</body>
</html>
There was a typo in your code. For assignment, it has to be g1 =
and not g1 ==
var ans; var b1; var g1; ans = prompt("How are you feeling?\\n'B' for Bad | 'G' for Good"); if (ans == 'B') { b1 = prompt("I'm sorry ... 'W'.\\nIf not, type 'E'"); if (b1 == 'W') { window.alert("Work sucks ...."); } else if (b1 == 'E') { window.alert("Here's .."); } } else if (ans == 'G') { g1 = prompt("That's great! I hope you have a fantastic day ahead!\\n'C' to Continue | 'R' to Exit"); if (g1 == 'C') { window.alert("..."); } else if (g1 == 'R') { window.alert("Goodbye. Have a lovely day!"); } }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.