直到今天,不得不做一个新的课程和 JS 使我感到困惑。
我的代码是这样,但我一直收到错误...
/*Instructions
- Greet your friend by printing a message to the console.
*/
*
var myFriend = "Sergia";
function greetings() {
console.log(`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;
}*
请帮帮我!
答案 0 :(得分:2)
您的 greetings
函数不返回任何要记录的内容,这就是它打印 undefined
的原因——这是不显式返回任何内容的函数的默认返回值。您应该做的是返回一个值,然后由调用 console.log
的 greetings()
记录该值。您不应该在函数中调用 console.log
。像这样:
/*Instructions
- Greet your friend by printing a message to the console.
*/
var myFriend = "Sergia";
function greetings() {
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;
}
答案 1 :(得分:1)
var myFriend = "Sergia";
function greetings() {
console.log(`Greetings ${myFriend}.`);
}
greetings();
u should not use console.log for greetings() function....It has console.log already inside the function....you should only call it
或
你可以试试:
var myFriend = "Sergia";
function greetings() {
return `Greetings ${myFriend} `
}
console.log(greetings());
答案 2 :(得分:0)
在 LC_ALL=C
函数中,您有 greetings
console.log(`Greetings ${myFriend}.`)
所以如果你 console.log 的函数像 function greetings() {
console.log(`Greetings ${myFriend}.`);
}
,它不会返回任何东西
所以你可以在像
这样的函数中使用console.log('results: ', greetings());
return