微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有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,");
        }
最佳答案
有两种方法可以解决此问题,但最简单的方法可能只是分配给结果变量.

    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可能不符合您的期望.看一下逻辑运算符,即&&和||

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐