将 if else 转换为 switch 语句
converted if else to switch statements
这是我到目前为止创建的代码,但无法正常工作。我只是一个初学者,我们正在创建这个 "guess your zodiac" 代码作为我们的学校作品之一。
请帮我改一下代码有什么问题:((
另外(对不起,如果我要求太多)我怎样才能在每种情况下插入一张照片,以便每个星座都有一张照片?
<html>
<body>
<h1> Guess your ZODIAC </h1>
<script>
window.confirm("HELLO");
window.confirm("I will try to determine your ZODIAC SIGN via this html. Cooperate with me :) ?");
window.confirm("oki, lego");
var zodiacmonth = prompt("Please insert your birthMONTH; if your birthday is on January 11, insert January");
var zodiacday = prompt("Please insert birthDAY; if your birthday is on January 11, insert 11")
switch (sign) {
case ((zodiacmonth == "February" && zodiacday >= 19) || (zodiacmonth == "March" && zodiacday <=20)):
window.confirm ("Your zodiac sign is PISCES");
break;
case ((zodiacmonth == "March" && zodiacday >= 21) || (zodiacmonth == "April" && zodiacday <=19)):
window.confirm ("Your zodiac sign is ARIES");
break;
case ((zodiacmonth == "April" && zodiacday >= 20) || (zodiacmonth == "May" && zodiacday <=20)):
window.confirm ("Your zodiac sign is TAURUS");
break;
case zodiacmonth == "May" && zodiacday >= 21 || zodiacmonth == "June" && zodiacday <=20 :
window.confirm ("Your zodiac sign is GEMINI");
break;
case zodiacmonth == "June" && zodiacday >= 21 || zodiacmonth == "July" && zodiacday <=22:
window.confirm ("Your zodiac sign is CANCER");
break;
case zodiacmonth == "July" && zodiacday >= 23 || zodiacmonth == "August" && zodiacday <=22 :
window.confirm ("Your zodiac sign is LEO");
break;
case zodiacmonth == "August" && zodiacday >= 23 || zodiacmonth == "September" && zodiacday <=22:
window.confirm ("Your zodiac sign is VIRGO");
break;
case zodiacmonth == "September" && zodiacday >= 23 || zodiacmonth == "October" && zodiacday <=22:
window.confirm ("Your zodiac sign is LIBRA");
break;
case zodiacmonth == "October" && zodiacday >= 23 || zodiacmonth == "November" && zodiacday <=21:
window.confirm ("Your zodiac sign is SCORPIO");
break;
case zodiacmonth == "November" && zodiacday >= 22 || zodiacmonth == "December" && zodiacday <=21:
window.confirm ("Your zodiac sign is SAGITTARIUS");
case zodiacmonth == "December" && zodiacday >= 22 || zodiacmonth == "January" && zodiacday <=19:
window.confirm ("Your zodiac sign is CAPRICORN");
break;
case zodiacmonth == "January" && zodiacday >= 20 || zodiacmonth == "February" && zodiacday <=18:
window.confirm ("Your zodiac sign is AQUARIUS, like mine :))");
break;
}
</script>
</body>
</html>
case
与 if(/else)
语句不同。
switch/case
块可用于检查 单个 变量的不同值。
您的条件包括多个变量,因此 switch/case
块在这里甚至不可能(/实际)。
就是说,如果您更好地构建数据,此代码 可以 优化:
var zodiacs = [
{ sign: "PISCES", monthA: "February", monthB: "March", dayA: 19, dayB: 20 },
{ sign: "ARIES", monthA: "March", monthB: "April", dayA: 21, dayB: 19 },
{ sign: "TAURUS", monthA: "April", monthB: "May", dayA: 20, dayB: 20 },
// etc
];
var zodiacmonth = prompt("Please insert your birthMONTH; if your birthday is on January 11, insert January");
var zodiacday = prompt("Please insert birthDAY; if your birthday is on January 11, insert 11")
var zodiac = zodiacs.filter(function(z){
return zodiacmonth == z.monthA && zodiacday >= z.dayA ||
zodiacmonth == z.monthB && zodiacday <= z.dayB;
})[0]
console.log(zodiac);
window.confirm ("Your zodiac sign is " + zodiac.sign);
使月份比较不区分大小写也可能有用:
zodiacmonth = zodiacmonth.toLowercase();
// and
{ sign: "PISCES", monthA: "february", monthB: "march", dayA: 19, dayB: 20 },
// ^ ^
这是我到目前为止创建的代码,但无法正常工作。我只是一个初学者,我们正在创建这个 "guess your zodiac" 代码作为我们的学校作品之一。
请帮我改一下代码有什么问题:((
另外(对不起,如果我要求太多)我怎样才能在每种情况下插入一张照片,以便每个星座都有一张照片?
<html>
<body>
<h1> Guess your ZODIAC </h1>
<script>
window.confirm("HELLO");
window.confirm("I will try to determine your ZODIAC SIGN via this html. Cooperate with me :) ?");
window.confirm("oki, lego");
var zodiacmonth = prompt("Please insert your birthMONTH; if your birthday is on January 11, insert January");
var zodiacday = prompt("Please insert birthDAY; if your birthday is on January 11, insert 11")
switch (sign) {
case ((zodiacmonth == "February" && zodiacday >= 19) || (zodiacmonth == "March" && zodiacday <=20)):
window.confirm ("Your zodiac sign is PISCES");
break;
case ((zodiacmonth == "March" && zodiacday >= 21) || (zodiacmonth == "April" && zodiacday <=19)):
window.confirm ("Your zodiac sign is ARIES");
break;
case ((zodiacmonth == "April" && zodiacday >= 20) || (zodiacmonth == "May" && zodiacday <=20)):
window.confirm ("Your zodiac sign is TAURUS");
break;
case zodiacmonth == "May" && zodiacday >= 21 || zodiacmonth == "June" && zodiacday <=20 :
window.confirm ("Your zodiac sign is GEMINI");
break;
case zodiacmonth == "June" && zodiacday >= 21 || zodiacmonth == "July" && zodiacday <=22:
window.confirm ("Your zodiac sign is CANCER");
break;
case zodiacmonth == "July" && zodiacday >= 23 || zodiacmonth == "August" && zodiacday <=22 :
window.confirm ("Your zodiac sign is LEO");
break;
case zodiacmonth == "August" && zodiacday >= 23 || zodiacmonth == "September" && zodiacday <=22:
window.confirm ("Your zodiac sign is VIRGO");
break;
case zodiacmonth == "September" && zodiacday >= 23 || zodiacmonth == "October" && zodiacday <=22:
window.confirm ("Your zodiac sign is LIBRA");
break;
case zodiacmonth == "October" && zodiacday >= 23 || zodiacmonth == "November" && zodiacday <=21:
window.confirm ("Your zodiac sign is SCORPIO");
break;
case zodiacmonth == "November" && zodiacday >= 22 || zodiacmonth == "December" && zodiacday <=21:
window.confirm ("Your zodiac sign is SAGITTARIUS");
case zodiacmonth == "December" && zodiacday >= 22 || zodiacmonth == "January" && zodiacday <=19:
window.confirm ("Your zodiac sign is CAPRICORN");
break;
case zodiacmonth == "January" && zodiacday >= 20 || zodiacmonth == "February" && zodiacday <=18:
window.confirm ("Your zodiac sign is AQUARIUS, like mine :))");
break;
}
</script>
</body>
</html>
case
与 if(/else)
语句不同。
switch/case
块可用于检查 单个 变量的不同值。
您的条件包括多个变量,因此 switch/case
块在这里甚至不可能(/实际)。
就是说,如果您更好地构建数据,此代码 可以 优化:
var zodiacs = [
{ sign: "PISCES", monthA: "February", monthB: "March", dayA: 19, dayB: 20 },
{ sign: "ARIES", monthA: "March", monthB: "April", dayA: 21, dayB: 19 },
{ sign: "TAURUS", monthA: "April", monthB: "May", dayA: 20, dayB: 20 },
// etc
];
var zodiacmonth = prompt("Please insert your birthMONTH; if your birthday is on January 11, insert January");
var zodiacday = prompt("Please insert birthDAY; if your birthday is on January 11, insert 11")
var zodiac = zodiacs.filter(function(z){
return zodiacmonth == z.monthA && zodiacday >= z.dayA ||
zodiacmonth == z.monthB && zodiacday <= z.dayB;
})[0]
console.log(zodiac);
window.confirm ("Your zodiac sign is " + zodiac.sign);
使月份比较不区分大小写也可能有用:
zodiacmonth = zodiacmonth.toLowercase();
// and
{ sign: "PISCES", monthA: "february", monthB: "march", dayA: 19, dayB: 20 },
// ^ ^