简体   繁体   English

我正在尝试解决的初学者 Javascript 函数问题

[英]Beginner Javascript function problem I'm trying to solve

The question I'm trying to solve goes as follows:我试图解决的问题如下:

I often struggle to know whether I should wear shorts or pants on a given day.我经常不知道在某一天我应该穿短裤还是裤子。 Please help me decide by writing me a function called isShortsWeather请帮我写一个叫做isShortsWeather的函数来决定

It should accept a single number argument, which we will call temperature (but you can name it whatever you want).它应该接受一个数字参数,我们将其称为temperature (但您可以随意命名)。

  • If temperature is greater than or equal to 75, return true .如果temperature大于或等于 75,则返回true
  • Otherwise, return false否则返回false
  • This exercise assumes temperature is in Fahrenheit此练习假设temperature为华氏度

Expected result:预期结果:

isShortsWeather(80) //true
isShortsWeather(48) //false
isShortsWeather(75) //true

The code I wrote is:我写的代码是:

function isShortsWeather(temperature) {
    if (temperature < 75); {
        return false;
    } 
    if (temperature >= 75) {
        return true;
    }
}

As snippet:作为片段:

 function isShortsWeather(temperature) { if (temperature < 75); { return false; } if (temperature >= 75) { return true; } } console.log(isShortsWeather(80)) //true console.log(isShortsWeather(48)) //false console.log(isShortsWeather(75)) //true

Please help me out by informing me what is wrong with my code and how should I fix this issue.请通过告诉我我的代码有什么问题以及我应该如何解决这个问题来帮助我。 I feel like I am relatively close to solving it.我觉得我比较接近解决它。 Thanks!谢谢!

只需返回布尔值:

return temperature >= 75;

the main reason it's not working is because you have a extra ;它不起作用的主要原因是因为你有一个额外的; after the first condition.在第一个条件之后。 you can shorten the function body to one line您可以将函数体缩短为一行

function isShortsWeather(temperature) {
    return temperature >= 75;
}

you forgot ;你忘记了; at the line 2, if you remove it it will work.在第 2 行,如果您将其删除,它将起作用。 Also it would be better if you make the 2nd if statement in else if此外,如果您在else if进行第二个 if 语句会更好

    function isShortsWeather(temperature) {
        if (temperature < 75) {
            return false;
        } else if (temperature >= 75) {
            return true;
    }

I would recommend a return on both if statements and not a console log on false.我建议同时返回 if 语句,而不是控制台登录 false。 You will use console.log to call the function.您将使用 console.log 来调用该函数。 I have edited the code a bit as the semicolon on line 2 not needed.由于不需要第 2 行的分号,我对代码进行了一些编辑。

 function isShortsWeather(temperature) { if (temperature < 75) { return false; } else { return true; } } temperature = 74; console.log(isShortsWeather(temperature));

Below you will find the correct code that is working.您将在下面找到正在运行的正确代码。 Hope answer your questions.希望回答你的问题。

 function isShortsWeather(temperature) { if (temperature >= 75) { return true; } else { return false; } } console.log(isShortsWeather(76)); console.log(isShortsWeather(74));

You can return the value instead of console logging it.您可以返回值而不是控制台记录它。

It would also be a good idea to refactor the "magic number" 75 into a variable with a default value, allowing you to easily change or override it later.将“幻数” 75重构为具有默认值的变量也是一个好主意,以便您以后轻松更改或覆盖它。

function isShortsWeather(temperature, cutoffTemp = 75) {
  if (temperature < cutoffTemp) {
    return false;
  } 
  if (temperature >= cutoffTemp) {
    return true;
  }
}

console.log(isShortsWeather(81));
function isShortsWeather(temperature) {

if (temperature < 75); {return 0;}

if (temperature >= 75) {return 1;}
}

I think that will solve your problem我认为这会解决你的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM