【问题标题】:How do I write a function starting with the keywork const - JavaScript?如何编写以键 const - JavaScript 开头的函数?
【发布时间】:2019-10-01 12:17:44
【问题描述】:

希望你一切安好。

我对编码非常陌生,我正在尝试完成一个挑战,我必须创建一个函数并将以下内容输出到控制台:

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

我没有得到的部分是简报要求我以关键字“const”(然后是 const 名称)开头。 我已经编写了以下输出所需内容的内容,但我不确定这是否正确。

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

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

简报中要求我编写一个带有两个参数(国家和天气类型)的函数,然后有两个参数(国家名称和天气类型)。

我会以“国家”的天气是“天气类型”的格式将其输出到控制台。

任何帮助将不胜感激。 非常感谢。

【问题讨论】:

  • 嗯...const getWeather = function (.....
  • 阅读一下术语“函数表达式”和“函数声明”
  • 感谢反馈的人。我在上面。

标签: javascript function console constants


【解决方案1】:

Functions 是 JS 中的一等公民,这意味着它可以分配给变量并作为参数传递或从函数返回函数,因此您可以将函数分配给变量并使用它

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

【讨论】:

  • 好的,我知道我哪里出错了 Code Maniac 。 “将函数分配给变量”是您声明中的关键。我有一个变化,但语法是错误的。我正要到达那里。感谢您的帮助和所有的建议。你生活和学习......我确实说过我很新:)
猜你喜欢
  • 1970-01-01
  • 2021-03-29
  • 1970-01-01
  • 2021-01-06
  • 1970-01-01
  • 2016-02-04
  • 2021-12-31
  • 2011-04-19
  • 2013-07-11
相关资源
最近更新 更多