Why the h... is 0 undefined in Javascript- it bothers me for many days


Hi! I am on my attempt to learn Javascript. I have had issues of trying learning programming before and I kinda felt, it wasn’t for me and I quit (c# was not for me…). Anyways, I am back and relying my focus on Javascript. I am on the Javascript fundamentals course and it’s eating up my nerves as from the time-time I meet the same kind of obstacle. When I declare a var and set it to 0, it makes my code to be undefined. I mean I start adding up values to it via += ; and it doesn’t work though logicly it should be.

One of the examples is:
let totalSleepHours=0;
totalSleepHours +=getSleepHours(‘monday’);
totalSleepHours +=getSleepHours(‘tueday’);
totalSleepHours +=getSleepHours(‘wednesday’);
totalSleepHours +=getSleepHours(‘thursday’);
totalSleepHours +=getSleepHours(‘friday’);
totalSleepHours +=getSleepHours(‘saturday’);
totalSleepHours +=getSleepHours(‘sunday’);
return totalSleepHours;

That’s the sleep calculator, which is messed up in the middle of the code. I watched the tutorial video and it gave me that edit:

const getActualSleepHours = () =>
return getSleepHours(‘Monday’) +
getSleepHours(‘Tuesday’) +
getSleepHours(‘Wednesday’) +
getSleepHours(‘Thuesday’) +
getSleepHours (‘Sunday’);


Here is the full code currently:

function getSleepHours (day)
if (day === ‘monday’)
{return 8;}
else if (day === ‘tuesday’)
{return 8}
else if (day === ‘wednesday’)
{return 8;}
else if (day === ‘thursday’)
{return 8;}
else if (day === ‘friday’)
{return 8;}
else if (day === ‘saturday’)
{return 8;}
else if (day === ‘sunday’)
{return 8;}
//console.log(getSleepHours(‘thursday’)) that piece of code is working and responsive

const getActualSleepHours = () => 
return getSleepHours('Monday') + 
getSleepHours('Tuesday') + 
getSleepHours('Wednesday') +
getSleepHours('Thuesday') +
getSleepHours ('Sunday');

/* var totalSleepHours = new Array (‘day’, number);

/*let totalSleepHours=0;
totalSleepHours +=getSleepHours('monday');
totalSleepHours +=getSleepHours('tueday');
totalSleepHours +=getSleepHours('wednesday');
totalSleepHours +=getSleepHours('thursday');
totalSleepHours +=getSleepHours('friday');
totalSleepHours +=getSleepHours('saturday');
totalSleepHours +=getSleepHours('sunday');
 return totalSleepHours;*/

// testing totalSleepHours

function getIdealSleepHours () {
var idealHours=8;
return idealHours*7;

function calculateSleepDebt () {
var actualSleepHours=getActualSleepHours();
var idealSleepHours=getIdealSleepHours();

if ( actualSleepHours == idealSleepHours)
console.log(“perfect amount of sleep”);
else if ( actualSleepHours > idealSleepHours)
console.log(“you sleep more than you need”);
console.log ( “your sleep excessive abundance is” + (actualSleepHours - idealSleepHours).toString);
else if ( actualSleepHours < idealSleepHours)
console.log(“you sleep less than you need”);
console.log ( “your lack of sleep is” + (idealSleepHours - actualSleepHours).toString);

My questions are:

  1. what exactly is wrong that it makes it undefined, when I used the first approach of (indeed I started with var as I dislike let and const declarators because they seem to rigid-I love flexibility) :
    var totalSleepHours=0;
    totalSleepHours +=getSleepHours(‘monday’);
    totalSleepHours +=getSleepHours(‘tueday’);
    totalSleepHours +=getSleepHours(‘wednesday’);
    totalSleepHours +=getSleepHours(‘thursday’);
    totalSleepHours +=getSleepHours(‘friday’);
    totalSleepHours +=getSleepHours(‘saturday’);
    totalSleepHours +=getSleepHours(‘sunday’);
    return totalSleepHours;

  2. How can I get this code to work by add up to zero and make it not undefined but defined by number approach?

  3. How can or should I approach to these situations or handle these things in the future to avoid such a nerve eating feeling because my code doesn’t wotk and I just don’t get what’s wrong and I have tried to solve it my own from one excercise to another and it cost me like 2 days facing the same issue in several excercises throughout that course?

You may consider me a noob, but I passed the function quize at 100%. That shows, I have the basic understanding of these things. I have written functions in c# many years ago, it’s not that weird in javascript. It even makes more sense in javascript in one hand, in the other hands it’s still a bit weird to me of getting the data out of function view-angle way.

Umm, I basicly am a noob, but I am not an idiot. I think I know the type of the error which is 0 turning to undefined. I just don’t know how to solve. So don’t be really harsh on me as it’s one of the basic questions you see all the time. I am asking this, because I know, if I can’t out of doing this mistake again and again, it will be with me all the time I program in javascript. So I need to solve this and I hope to get help in here.Thanks in advance btw!


This needs a body.

const getActualSleepHours = () => {
    return // ...


0 isn’t undefined, you’re doing a whole lot of things and somewhere you drop the ball.
start adding prints to narrow down where. if, by the time you’ve narrowed it down to a single operation and it’s still a mystery then that’s something to ask about


I added {}. console spit out undefined…


Let’s have another look at your complete body of code for this exercise.


That tells me nothing, but I also don’t think I need to know anything here, it’s you who needs to look at what is happening in your code and that is something you can do by printing out information from within the program - your program is not some heavily guarded secret fortress, you can print out whatever you want and you can therefore find out where exactly you are getting the value undefined from

If you somehow manage to get somebody else to find that out for you, then they will have done it by printing things out or reading the code very carefully - so that is what you should be doing, nobody needs to do it for you

Something else you can do is to work with less code. Remove half or whatever amount until you have an amount of code that you are comfortable handling. Keep a copy, obviously.

In your first post you say it’s got to do with +=, if that is true then you will find your issue by looking at each individual part that you are using there. Break it up into small pieces and examine them individually, find out what happens.