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
相关产品推荐
相关产品推荐

