switch 块中的 If 语句 javascript
If statement in a switch block javascript
是否可以在 Javascript 的 switch 块中使用来自 switch 块的响应的 if/else 语句?以下是我这样做的尝试。这是一个 CodeAcademy 项目,您必须在其中使用开关块和条件语句编写自己的冒险故事。
var user = prompt("You awake; dazed, blurred and lost. What do you do?").toLowerCase()
var day = 0;
switch (user) {
case "look at the time":
console.log("Good start, how long was I out for?");
if (
case "look at the time" && day === 0) {
console.log("it's only been a few hours, but time is still ticking");
day++
} else if (day > 0) {
console.log("it's been " + day + "s! Will I make it out of here");
day++
}
break;
case "figure out where I am":
console.log("Yeah, how did I end up here in the first place?");
if (
case "figure out where I am" && day === 0) {
console.log("This is a good checkpoint");
day++
}
break;
case 'run':
console.log("Dry your eyes, find a direction, and hope for the best");
break;
case 'give up':
console.log('Thanks for trying, have fun, where ever you are...');
break;
default:
console.log("you fall back unconscious, hoping to wake again");
day++;
if (
default) {
return user;
}
if (
case 'run' ||
case 'give up') {
console.log("It would have ended up coming down to this option anyway");
}
}
如果您进行更改以停止在 if
语句中使用单词 case
,它将起作用,例如它应该看起来像这样与 user
进行比较来自 case 语句的变量:
if (user === "look at the time" && day === 0) {
console.log("it's only been a few hours, but time is still ticking");
day++
} else if (day > 0) {
console.log("it's been " + day + "s! Will I make it out of here");
day++
}
在这种情况下,比较 user === "look at the time"
是多余的,因为该逻辑已经在 switch 语句本身中进行了评估。我只是将它包括在内,以便您可以看到如何与 user
变量进行比较,但它可以完全排除在这种情况下并具有相同的效果。
是否可以在 Javascript 的 switch 块中使用来自 switch 块的响应的 if/else 语句?以下是我这样做的尝试。这是一个 CodeAcademy 项目,您必须在其中使用开关块和条件语句编写自己的冒险故事。
var user = prompt("You awake; dazed, blurred and lost. What do you do?").toLowerCase()
var day = 0;
switch (user) {
case "look at the time":
console.log("Good start, how long was I out for?");
if (
case "look at the time" && day === 0) {
console.log("it's only been a few hours, but time is still ticking");
day++
} else if (day > 0) {
console.log("it's been " + day + "s! Will I make it out of here");
day++
}
break;
case "figure out where I am":
console.log("Yeah, how did I end up here in the first place?");
if (
case "figure out where I am" && day === 0) {
console.log("This is a good checkpoint");
day++
}
break;
case 'run':
console.log("Dry your eyes, find a direction, and hope for the best");
break;
case 'give up':
console.log('Thanks for trying, have fun, where ever you are...');
break;
default:
console.log("you fall back unconscious, hoping to wake again");
day++;
if (
default) {
return user;
}
if (
case 'run' ||
case 'give up') {
console.log("It would have ended up coming down to this option anyway");
}
}
如果您进行更改以停止在 if
语句中使用单词 case
,它将起作用,例如它应该看起来像这样与 user
进行比较来自 case 语句的变量:
if (user === "look at the time" && day === 0) {
console.log("it's only been a few hours, but time is still ticking");
day++
} else if (day > 0) {
console.log("it's been " + day + "s! Will I make it out of here");
day++
}
在这种情况下,比较 user === "look at the time"
是多余的,因为该逻辑已经在 switch 语句本身中进行了评估。我只是将它包括在内,以便您可以看到如何与 user
变量进行比较,但它可以完全排除在这种情况下并具有相同的效果。