0
是否可以在Javascript中的開關塊內使用來自開關塊的響應使用if/else語句?以下是我的嘗試。這是一個CodeAcademy項目,您必須使用開關塊和條件語句對自己的冒險進行編碼。如果語句在開關塊中javascript
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");
}
}
你不需要再次檢查(並且不能)'case',只需檢查一天。 – Teemu
不,不能將if語句放在'switch'塊中。你只能在那裏放置'case'。而且你不能在'if'條件下放置'case's。這只是一個語法錯誤。 – Bergi