简体   繁体   English

当我尝试在函数中添加一些结果时,为什么 console.log 打印到控制台“未定义”?

[英]Why the console.log prints to the console "undefined" when I try to add some results in the function?

I try to add getSleepHours() in the const getActualSleepHours() to have a sum.我尝试在 const getActualSleepHours() 中添加 getSleepHours() 以获得总和。 But the console.log prints undefined and I don't know what I'm doing wrong.但是 console.log 打印 undefined ,我不知道我做错了什么。 Could anyone help me?有人可以帮助我吗?

 const getSleepHours = day => { switch(day) { case 'monday': return 8; break; case 'tuesday': return 8; break; case 'wednesday': return 9; break; case 'thursday': return 9; break; case 'friday': return 7; break; case 'saturday': return 10; break; case 'sunday': return 9; break; } }; const getActualSleepHours = () => { getSleepHours('monday') + getSleepHours('tuesday') + getSleepHours('wednesday') + getSleepHours('thursday') + getSleepHours('friday') + getSleepHours('saturday') + getSleepHours('sunday'); }; const getIdealSleepHours = () => { const idealHours = 8.5; return idealHours * 7; }; console.log(getActualSleepHours()); console.log(getIdealSleepHours());

You missed the return, the function you're calling has no return value so it returns what every executed JS function with no return value returns, undefined您错过了返回,您调用的函数没有返回值,因此它返回每个执行的 JS 函数没有返回值返回的内容, undefined

 const getSleepHours = day => { switch(day) { case 'monday': return 8; break; case 'tuesday': return 8; break; case 'wednesday': return 9; break; case 'thursday': return 9; break; case 'friday': return 7; break; case 'saturday': return 10; break; case 'sunday': return 9; break; } }; const getActualSleepHours = () => getSleepHours('monday') + getSleepHours('tuesday') + getSleepHours('wednesday') + getSleepHours('thursday') + getSleepHours('friday') + getSleepHours('saturday') + getSleepHours('sunday'); const getIdealSleepHours = () => { const idealHours = 8.5; return idealHours * 7; }; console.log(getActualSleepHours()); console.log(getIdealSleepHours());

You're not returning anything in your getActualSleepHours function.您没有在getActualSleepHours函数中返回任何内容。 Get rid of the braces so it goes from摆脱大括号,所以它从

const getActualSleepHours = () => {
    getSleepHours("monday") +
    getSleepHours("tuesday") +
    getSleepHours("wednesday") +
    getSleepHours("thursday") +
    getSleepHours("friday") +
    getSleepHours("saturday") +
    getSleepHours("sunday");
};

to

const getActualSleepHours = () =>
    getSleepHours("monday") +
    getSleepHours("tuesday") +
    getSleepHours("wednesday") +
    getSleepHours("thursday") +
    getSleepHours("friday") +
    getSleepHours("saturday") +
    getSleepHours("sunday");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在console.log内部具有控制台日志输出的函数将输出未定义的 - A function with a console log output inside console.log prints undefined 为什么console.log函数返回undefined? - Why console.log function returns undefined? 为什么当我 console.log() 时它说未定义? - Why does it say undefined when i console.log()? 属性未定义但 console.log() 打印属性 - Property undefined but the console.log() prints the property console.log()打印出函数 - console.log() prints out function Javascript,console.log打印prints对象,但属性未定义 - Javascript, console.log prints prints object, but property is undefined 为什么我console.log一个对象,它显示对象,但是当我console.log时,它显示未定义 - Why I console.log a Object,it shows object,but when I console Object.value ,it shows undefined 为什么即使在console语句之后更新了数组,console.log为什么也打印数组? - Why does console.log prints array even when the array is updated after the console statements? javascript 量角器函数在从黄瓜 stepdefinition 调用时返回 undefined 即使 console.log() 打印正确的值 - javascript protractor function returns undefined when called from a cucumber stepdefinition even though console.log()prints correct values 当我的console.log类型的函数返回一个对象时,它的类型为undefined。 我不明白为什么 - When I console.log type of my function which returns an object it gives undefined. I don't understand why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM