简体   繁体   English

如何从键 const - JavaScript 开始编写 function?

[英]How do I write a function starting with the keywork const - JavaScript?

I hope you are well.我希望你一切都好。

I am very new to coding and I am trying to complete a challenge where I have to create a function and output the following to console:我对编码很陌生,我正在尝试完成一个挑战,我必须创建一个 function 和 output 以控制台:

The weather in Scotland is sunny
The weather in Spain is glorious
The weather in Poland is cold
=> undefined 

The part I am not getting is that the brief asks me to start with the keyword 'const' (then the const name).我没有得到的部分是简报要求我以关键字“const”(然后是 const 名称)开头。 I have written the following which outputs what is required but I'm not sure if this is correct.我写了以下输出所需的内容,但我不确定这是否正确。

function getWeather (country, weatherType) {
  console.log('The weather in ' + country + ' is ' + weatherType);
}

getWeather('Scotland', 'sunny');
getWeather('Spain', 'glorious');
getWeather('Poland', 'cold');

I am being asked in the brief to write a function with two parameters (country & weatherType) and then have two arguments (the name of a country & the type of weather).简报中要求我编写一个带有两个参数(国家和天气类型)的 function,然后有两个 arguments(国家名称和天气类型)。

I will output this to console in the format 'The weather in 'country' is 'type of weather.我将 output 以“国家”的天气是“天气类型”的格式进行控制台。

Any help would be appreciated.任何帮助,将不胜感激。 Many thanks.非常感谢。

Functions are first class citizens in JS means it can be assigned to variable and passed as argument or return a function from function, so you can assign a function to variable and use it Functions are first class citizens in JS means it can be assigned to variable and passed as argument or return a function from function, so you can assign a function to variable and use it

First class function MDN

 const getWeather = function (country, weatherType) { console.log('The weather in ' + country + ' is ' + weatherType); } getWeather('Scotland', 'sunny'); getWeather('Spain', 'glorious'); getWeather('Poland', 'cold');

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

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