You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript编写greetings函数返回指定字符串报错问题求解

任务说明
本次练习中,你需要在JavaScript函数内定义变量,后续我们会深入讲解函数相关知识,本次重点关注变量使用。
完成该任务需执行以下操作:

  • 打开Variables-01文件夹,在variables01.js文件中编写代码实现以下需求
  • 使用任意变量声明方式创建名为myFriend的变量
  • 将该变量赋值为你朋友的姓名
  • 在greetings函数内部返回字符串:"Greetings [你朋友的姓名]"

错误点梳理

  • 函数定义了冗余入参,其中对象解构参数{myFriend}在无入参调用时会直接抛出类型错误
  • 生成的目标格式字符串没有通过return返回,函数默认返回undefined,不符合任务要求
  • 模板字符串中问候语首字母为小写,和要求的Greetings格式不一致

修正后代码

/*Instructions
 - Greet your friend by printing a message to the console.
*/
function greetings() {
  var myFriend = 'Mike';
  return `Greetings ${myFriend}`;
}

//leave this line unchanged to console log the results
console.log('results: ', greetings());

//don't change this line
if (typeof module !== 'undefined') {
  module.exports = greetings;
}

内容的提问来源于stack exchange,提问作者HackAttack101

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.11 19:57:01