简体   繁体   中英

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:

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). 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).

I will output this to console in the format 'The weather in 'country' is 'type of weather.

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

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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