Console.log not logging to console

Please see below but the console.log’s at the end of the script don’t log anything but produce no error!

const getsleephours = day => {

 switch(day) {
 case 'monday':
 return 8
 break;
 case 'tuesday':
 return 7
 break;
 case 'wednesday':
 return 7
 break;
 case 'thursday':
 return 7
 break;
 case 'friday':
 return 7
 break;
 case 'saturday':
 return 7
 break;
 case 'sunday':
 return 7
 break;
 default:
 return 'error'
 }

};

const getActualSleepHours = () => {
return getSleepHours(‘monday’) +
getSleepHours(‘tuesday’) +
getSleepHours(‘wednesday’) +
getSleepHours(‘thursday’) +
getSleepHours(‘friday’) +
getSleepHours(‘saturday’) +
getSleepHours(‘sunday’);
console.log(getSleephours(‘monday’))
console.log(getActualSleepHours())
}

Hi there and welcome to the forums!

Your console.log() statements are sitting within the getActualSleepHours() function. This means that without a function call, all the code within that function does not run, including any console.log() statements. You’ll need to call the function below it before you will see any output from your script.

const getsleephours = day => { //not in camelCaseFormat eg. getSleepHours

thanks i will give that a try

thanks i will give that a try but it may be a simple error off another kind

I Tried typing out exactly what was instructed in the supplied video for the sleep debt calculator again and it still didn’t work. in fact it appears to not log anything to the console but with no error. I may have misunderstood your comment as im a noobie but could you look at the code again and see if i have done it again. I just cannot see where I can Input the console.log’s and the one’s in the code below don’t work either

const getSleepHours = day => {

 switch(day) {
 case 'monday':
 return 8
 break;
 case 'tuesday':
 return 7
 break;
 case 'wednesday':
 return 7
 break;
 case 'thursday':
 return 7
 break;
 case 'friday':
 return 5
 break;
 case 'saturday':
 return 7
 break;
 case 'sunday':
 return 7
 break;
 default:
 return 'error'
 }

};

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

const getIdealSleepHours = () => {
let idealSleepHours = 8;
return idealHours * 7;
}

const calculateSleepDebt = () => {
const actualSleepHours = getactualSleepHours();
const idealSleepHours = getidealSleepHours();
if(actualSleepHours === idealSleepHours){
console.log(“perfect”);
}
else if(actualSleepHours > idealSleepHours){
console.log(“more”);
}
else if(actualSleepHours < idealSleepHours){
console.log(“less”);
}
};

A couple problems to observe. Firstly, the reason you get no console.log() outputs is because they are in function definitions, and you haven’t put any function calls. Writing a function on its own does not run the code contained within, it’s essentially a template so the code knows that when you write calculateSleepDebt(), you want it to execute all of the code contained within it. Therefore if you want to see the console.log() outputs that are in the function, you need to call it at the end, like below;

const calculateSleepDebt = () => {
const actualSleepHours = getactualSleepHours();
const idealSleepHours = getidealSleepHours();
if(actualSleepHours === idealSleepHours){
console.log(“perfect”);
}
else if(actualSleepHours > idealSleepHours){
console.log(“more”);
}
else if(actualSleepHours < idealSleepHours){
console.log(“less”);
}
};

// This is the important extra bit, the function call
calculateSleepDebt()

without this function call, no code within the block actually runs.

There is also a couple problems with your code that will flag up errors, one example is this line;

You need to be careful about getting your capitalisation correct when calling the functions you have written, as getactualSleepHours() is not the same as getActualSleepHours(). There’s a couple instances of misspelling and missing words throughout the code that will flag undefined errors for you, so keep running and see if you can figure that out yourself. If not, let me know and I’d be happy to help more!

2 Likes

thanks I have resolved my issue and thank you for the help