SleepDebt Calculator

Hello,
So, I even watched the video and followed it step by step, but I kept getting an error after I logged:const getIdealSleepHours = () => {
let idealHours = 8;
return idealHours * 7;
};

Now, I even went line by line and reentered the code, but nothing seems to work to get the function calculateSleepDebt(); to run and to produce an answer…Am I missing something? Did I overlook punctuation? Did I overlook a spelling?

I keep getting this error:
/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/sleepDebtCalculator.js:46
getidealSleepHours();
^

ReferenceError: getidealSleepHours is not defined
at calculateSleepDebt (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/sleepDebtCalculator.js:46:9)
at Object. (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/sleepDebtCalculator.js:58:2)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

Below is the code I logged. Thank you

const getSleepHours = day => {
switch (day) {
case ‘monday’:
return 8
break;
case ‘tuesday’:
return 7
break;
case ‘wednesday’:
return 6
break;
case ‘thursday’:
return 5
break;
case ‘friday’:
return 4
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’);

console.log(getSleepHours(‘monday’));
console.log(getActualSleepHours());

const getIdealSleepHours = () => {
let idealHours = 8;
return idealHours * 7;
};
const calculateSleepDebt = () => {
let actualSleepHours =
getActualSleepHours();
let idealSleepHours =
getidealSleepHours();
if(actualSleepHours === idealSleepHours){
console.log(“You’ve gotten the perfect amonut of sleep.”);
}
else if(acutalSleepHours > idealSleepHours){
console.log(“You’ve got” + (idealSleepHours -actualSleepHours) + “more hours of sleep this week.”);
}
else if(actualSleepHours < idealSleepHours){ console.log(“You should get some rest because you’ve slept” + (idealSleepHours - actualSleepHours ) + “hours less than you should this week.”); }
else {
console.log(“Error! Something went wrong. Check your code.”);
}
};
calculateSleepDebt();

You have a capital I after get in every case except this one. Should be getIdealSleepHours()

Thank you, I am going to try the exercise again.

1 Like

Hi There, on the project sleepCalculator; I have a couple of concerns with my project outcome. Here is my code below:

const getSleepHours = (day) => {
	switch (day) {
  	case 'monday':
  		console.log(5);
  		break;
    case 'tuesday':
    	console.log(6);
      break;
    case 'wednesday':
    	console.log(7);
      break;
    case 'thursday':
    	console.log(6);
      break;
    case 'friday':
    	console.log(5);
      break;
    case 'saturday':
     console.log(8);
     break;
    case 'sunday':
    	console.log(9);
      break;
    default:
    	console.log('You did not add anything');
      break;
  }
};

// A test to confirm the code prints Sunday's value
console.log(getSleepHours('sunday')); 


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

const getIdealSleepHours = () => {
	const idealHours = 10; // set my ideal hours of sleep
	return idealHours * 7;
};

// Now we are testing:
console.log(getActualSleepHours());
console.log(getIdealSleepHours());

// Now we can calculate our sleep allowance:
const calculateSleepDebt = () => {
	const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours();
  
  if (actualSleepHours === idealSleepHours){
    console.log('You got the perfect amount of sleep');
  } else if(actualSleepHours > idealSleepHours){
    console.log('You got more sleep than needed');
    console.log('You had an extra ' + (actualSleepHours - idealSleepHours) + ' hours of sleep; which is way beyond your recommeded amount.')
    
  } else if (actualSleepHours < idealSleepHours){
    console.log('You need to get some rest');
    console.log('You only had ' + (idealSleepHours - actualSleepHours) + ' of sleep which is below your ideal');
  } else{
    console.log('Are you sure you have slept at all?');
  }
};

calculateSleepDebt();

When I ran the code this is what I got:

9
undefined
5
6
7
6
5
8
9
NaN
70
5
6
7
6
5
8
9
Are you sure you have slept at all?

My questions are:

  1. Why is the sum of the days not adding up rather it prints the on each line the numbers for the corresponding day of the week? As you can see; Nan is printed as well after printing Sunday. Does this mean I should have converted the numbers to integers before I can get the sum?

  2. Undefined is printed after 9 (where 9 is the test value for the day Sunday). Does this mean I should have converted the numbers to integers before I can get the sum?

  3. I noticed when I called the “calculateSleepDebt();” function nothing happened expect repeat the outcome. It did not even calculate the difference in sleep times

I would appreciate some explanations so I can understand where am getting it all mixed up.

Just incase, here is the link to my exercise as well Sleep Debt Calculator

Regards,

You need to replace console.log() with return in your case blocks. You are only printing the values to the console instead of returning them to the caller, so they can be added together. When you replace console.log() with return you will no longer need the break; statements. return will perform the same function as break. Your new switch block should look similar to this:

switch (day) {
  case 'monday':
    return 5;
  case 'tuesday':
    return 6;
//etc..

Hope this helps!

Thanks a lot, I will implement those changes are let you know how it goes.

Much appreciated.

Hi midlinder,

I implemented the changes and it worked. Just one question, does this mean that when I make use of the switch statement, console.log() cannot be used to make such calculations or is it a case of as best practice we make use of the return key word?

Regards

It totally depends on what you are trying to do. console.log() only prints whatever we put between the parenthesis to the console. If we want to do something with a value like in this project, we need to return the value to the line of code that does the mathematical operation.

Consider these examples which will hopefully shed some light on the difference between the two concepts.

Example 1

const addStuff = (num1, num2) => {
  total = num1 + num2;
  return total; // returns 5 to the caller
}

mySum = addStuff(2, 3);
console.log(mySum); // prints 5

Output:

5

Example 2

const addStuff = (num1, num2) => {
  total = num1 + num2;
  console.log(total); //prints 5
}

addStuff(2, 3); // calls the function

console.log(addStuff(2, 3)); // first addStuff(2, 3) is called and prints 5 then
//console.log() prints 'undefined' since the function addStuff() doesn't have a return statement

Output:

5
5
undefined

Example 3

const addStuff = (num1, num2) => {
  total = num1 + num2;
  console.log(total); //prints 5
  return "I finished adding your numbers."; //returns the string value to the caller
}

addStuff(2, 3); // calls the function

console.log(addStuff(2, 3)); // first addStuff(2, 3) is called and prints 5 then
//console.log() prints the value returned by the function

Output:

5
5
I finished adding your numbers.

So, you see that console.log() only prints while return only returns (hands data back to the caller).
Hopefully this helps.