尝试从提示输入console.log 2输入

时间:2019-03-21 12:12:05

标签: javascript

我有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,");
        }

1 个答案:

答案 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)

修改

其他一些注释,因为看来您可能仍在学习(顺便问一下第一个大问题!)

  • 为变量选择一个一致的大小写(我可能会将EventType重命名为eventType,因此与tempFahr的大小写相同)
  • 54 + >70可能不符合您的期望。查看逻辑运算符,即&&||