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

前端之家收集整理的这篇文章主要介绍了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可能不符合您的期望.看一下逻辑运算符,即&&和||

原文链接:https://www.f2er.com/js/531236.html

猜你在找的JavaScript相关文章