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(‘Friday’)+
getSleepHours(‘Saturday’)+
getSleepHours (‘Sunday’);
console.log(getActualSleepHours());
IT DOESNT WORK!
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;}
//test
//console.log(getSleepHours(‘thursday’)) that piece of code is working and responsive
}
const getActualSleepHours = () =>
return getSleepHours('Monday') +
getSleepHours('Tuesday') +
getSleepHours('Wednesday') +
getSleepHours('Thuesday') +
getSleepHours('Friday')+
getSleepHours('Saturday')+
getSleepHours ('Sunday');
console.log(getActualSleepHours());
/* var totalSleepHours = new Array (‘day’, number);
totalSleepHours.push(‘monday’,getSleepHours(‘monday’).toInt);
totalSleepHours.push(‘tuesday’,getSleepHours(‘tuesday’).toInt);
totalSleepHours.push(‘wednesday’,getSleepHours(‘wednesday’).toInt);
totalSleepHours.push(‘thursday’,getSleepHours(‘thursday’).toInt);
totalSleepHours.push(‘friday’,getSleepHours(‘friday’).toInt);
totalSleepHours.push(‘saturday’,getSleepHours(‘saturday’).toInt);
totalSleepHours.push(‘sunday’,getSleepHours(‘sunday’).toInt);
/*
/*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);
}
}
console.log(calculateSleepDebt());
My questions are:
-
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; -
How can I get this code to work by add up to zero and make it not undefined but defined by number approach?
-
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!