我有2个问题,我希望用户回答,并希望console.log将这些答案放在一起。我已经成功完成了1个问题,但无法弄清楚如何获得2个问题。我知道我还很遥远,但这是我到目前为止的成就。
var EventType = prompt("What kind of Event are you attending?");
var tempFahr = prompt("What is the temperature?");
if( EventType == "semi-formal" ) {
console.log("Wear a polo ");
} else if( EventType == "casual" ) {
console.log("Wear something comfy ");
} else if( EventType == "formal" ) {
console.log("Wear a suit");
} else {
console.log("Wear nothing!");
}
if( tempFahr <= 70 ) {
console.log("It is hot outside!");
} else if( tempFahr >= 54 ) {
console.log("It's chilly outside!");
} else if( tempFahr < 54 + >70 ) {
console.log("It is pleasant outside");
} else {
console.log("Who cares about the weather,");
}
答案 0 :(得分:2)
有两种方法可以解决此问题,但最简单的方法可能只是将其分配给结果变量。
var EventType = prompt("What kind of Event are you attending?");
var tempFahr = prompt("What is the temperature?");
var recommendedClothing
if( EventType == "semi-formal" ) {
recommendedClothing = "Wear a polo ";
} else if( EventType == "casual" ) {
recommendedClothing = "Wear something comfy ";
} else if( EventType == "formal" ) {
recommendedClothing = "Wear a suit";
} else {
recommendedClothing = "Wear nothing!";
}
var weatherAssessment
if( tempFahr <= 70 ) {
weatherAssessment = "It is hot outside!";
} else if( tempFahr >= 54 ) {
weatherAssessment = "It's chilly outside!";
} else if( tempFahr < 54 || tempFahr > 70 ) {
weatherAssessment = "It is pleasant outside";
} else {
weatherAssessment = "Who cares about the weather,";
}
console.log(recommendedClothing + ' ' + weatherAssessment)
修改
其他一些注释,因为看来您可能仍在学习(顺便问一下第一个大问题!)
54 + >70
可能不符合您的期望。查看逻辑运算符,即&&
和||