Sleep Debt Calculator -- Variables not storing/defining?

Hello fine people! Did anyone else have issues/problems with this task in that the variables (const) were not storing?

See the code below – the Calculate function was not reading the summed numbers for ideal and logged sleep; however, when logged directly to console are.

Hoping someone has seen before, thanks!

// Sleep Debt Calculator //
const getSleepHours = (day) => {
  if (day === "Monday") {
    return 6;
  } else if (day === "Tuesday") {
    return 6;
  } else if (day === "Wednesday") {
    return 6;
  } else if (day === "Thursday") {
    return 6;
  } else if (day === "Friday") {
    return 8;
  } else if (day === "Saturday") {
    return 8;
  } else if (day === "Sunday") {
    return 16;
  } else {
    return "error";
  }
};
// Calculate listed data
let getActualSleepHours = () =>
  getSleepHours("Monday") +
  getSleepHours("Tuesday") +
  getSleepHours("Wednesday") +
  getSleepHours("Thursday") +
  getSleepHours("Friday") +
  getSleepHours("Saturday") +
  getSleepHours("Sunday");

// Calculate IDEAL sleep
const getIdealSleepHours = () => {
  const idealHours = 8;
  return idealHours * 7;
};

// TESTING INPUTS
console.log(getIdealSleepHours());
console.log(getActualSleepHours());
// Input data logs to console.

// Calulate Sleep Debt
const Calculate = () => {
  const idealSleepHours = getIdealSleepHours();
  const actualSleepHours = getActualSleepHours();
};

// TESTING VARIABLES ?? Not logging to console (Remove console.log after running to call function)
console.log(IdealSleepHours());
console.log(ActualSleepHours());

// Calling
if (actualSleepHours === idealSleepHours) {
  console.log("You got sufficnent sleep!");
}
//
else if (actualSleepHours > idealSleepHours) {
  console.log(
    "You got " +
      (IdealSleepHours + ActualSleepHours) +
      " hour(s) MORE sleep than you needed this week. Get Up!!"
  );
}
//
else {
  actualSleepHours < idealSleepHours;
  console.log(
    "You got " +
      (IdealSleepHours - ActualSleepHours) +
      " hour(s) less sleep than you needed this week. Get some rest."
  );
}

// Calling Function
calculate();
//

The name of the variable was idealSleepHours , not IdealSleepHours , and it is a number, not a function.
So IdealSleepHours() would not work. (Capitalization matters in JavaScript.)

You declared idealSleepHours inside a function, so it would not be accessible outside that function.
(The variable idealSleepHours has function scope, not global scope.)

You could do that code outside the function (which is more straightforward), or at least declare the variable outside the function to make that work.

code for the latter
// Calulate Sleep Debt
let idealSleepHours;
let actualSleepHours;

const Calculate = () => {
  idealSleepHours = getIdealSleepHours();
  actualSleepHours = getActualSleepHours();
};

Also, the calculations must happen before you use the variables for comparisons. Not at the end.

1 Like

Thanks for checking this out! Much appreciated.

We’re getting there. The variables are being picked up however the math is not logging to console:

Your ideal sleep hours are 56
And your actual sleep hours are 50
You got enough sleep!

So we have no more errors, but the equations [
(actualSleepHours > idealSleepHours) ] don’t read :frowning:

I’m hoping it’s another simple case-sensitive thing…

From the below:

// Sleep Debt Calculator //
const getSleepHours = (day) => {
  if (day === "Monday") {
    return 6;
  } else if (day === "Tuesday") {
    return 6;
  } else if (day === "Wednesday") {
    return 6;
  } else if (day === "Thursday") {
    return 6;
  } else if (day === "Friday") {
    return 8;
  } else if (day === "Saturday") {
    return 8;
  } else if (day === "Sunday") {
    return 10;
  } else {
    return "error";
  }
};
// Calculate listed data
let getActualSleepHours = () =>
  getSleepHours("Monday") +
  getSleepHours("Tuesday") +
  getSleepHours("Wednesday") +
  getSleepHours("Thursday") +
  getSleepHours("Friday") +
  getSleepHours("Saturday") +
  getSleepHours("Sunday");
// Calculate IDEAL sleep?
const getIdealSleepHours = () => {
  const idealHours = 8;
  return idealHours * 7;
};
// Log Existing Numbers 
console.log("Your ideal sleep hours are " + getIdealSleepHours());
console.log("And your actual sleep hours are " + getActualSleepHours());
// Calulate Sleep Debt
let actualSleepHours;
let idealSleepHours;
const Calculate = () => {
  idealSleepHours = getIdealSleepHours();
  actualSleepHours = getActualSleepHours();
};
// Calculating Return 
if (actualSleepHours === idealSleepHours) {
  console.log("You got enough sleep!");
}
//
else if (actualSleepHours > idealSleepHours) {
  console.log(
    "You got " +
      (idealSleepHours + actualSleepHours) +
      " hour(s) MORE sleep than you needed this week. Get Up!!"
  );
}
//
 else if 
  (actualSleepHours < idealSleepHours) {
  console.log(
    "You got " +
      (idealSleepHours - actualSleepHours) +
      " hour(s) less sleep than you needed this week. Get some rest."
  );
  }
 // 
 else {
  console.log('Man, we need to sleep. Something not adding up...');
}
Calculate();
// End of Program //

(idealSleepHours + actualSleepHours)
should be
(actualSleepHours - idealSleepHours)

As an additional challenge, try to put all the if and else-if and else stuff into a function.

Hey! Thank you very much for the help above :slight_smile:

I’m still getting the following:
Your ideal sleep hours are 56
And your actual sleep hours are 50
You got enough sleep!
undefined
undefined

From:

// Sleep Debt Calculator //
const getSleepHours = (day) => {
    if (day === "Monday") {
      return 6;
    } else if (day === "Tuesday") {
      return 6;
    } else if (day === "Wednesday") {
      return 6;
    } else if (day === "Thursday") {
      return 6;
    } else if (day === "Friday") {
      return 8;
    } else if (day === "Saturday") {
      return 8;
    } else if (day === "Sunday") {
      return 10;
    } else {
      return "error";
    }
  };
  // Calculate listed data
  let getActualSleepHours = () =>
    getSleepHours("Monday") +
    getSleepHours("Tuesday") +
    getSleepHours("Wednesday") +
    getSleepHours("Thursday") +
    getSleepHours("Friday") +
    getSleepHours("Saturday") +
    getSleepHours("Sunday");
  // Calculate IDEAL sleep?
  const getIdealSleepHours = () => {
    const idealHours = 8;
    return idealHours * 7;
  };
  // Log Existing Numbers
  console.log("Your ideal sleep hours are " + getIdealSleepHours());
  console.log("And your actual sleep hours are " + getActualSleepHours());
  // Calulate Sleep Debt
  let actualSleepHours;
  let idealSleepHours;
  let Calculate = () => {
    idealSleepHours = getIdealSleepHours();
    actualSleepHours = getActualSleepHours();
  };
  // Calculating Return
  let CalculateSleep = () => {
    if (actualSleepHours === idealSleepHours) {
      console.log("You got enough sleep!");
    }
    //
    else if (actualSleepHours > idealSleepHours) {
      console.log(
        "You got " +
          (actualSleepHours - idealSleepHours) +
          " hour(s) MORE sleep than you needed this week. Get Up!!"
      );
    }
    //
    else if (actualSleepHours < idealSleepHours) {
      console.log(
        "You got " +
          (idealSleepHours - actualSleepHours) -
          " hour(s) less sleep than you needed this week. Get some rest."
      );
    }
    //
    else {
      console.log("Man, we need to sleep. Something not adding up...");
    }
  };
  // Call Function
  CalculateSleep();
  // Log Inputs //
  console.log(actualSleepHours);
  console.log(idealSleepHours);
  // End of Program //

I switched to Visual Studio to make sure all variables can be traced back to the original deceleration, but that didn’t work either.

Anyway, I do thank you for your time and I did apply a set function for all else-if as recommend. Hope you have a great evening and not getting rained out like us central BKers…

You need to call the Calculate function before the CalculateSleep function is called,
or call the Calculate function at the beginning of the CalculateSleep function;
so that the variables idealSleepHours and actualSleepHours are calculated before the if and else-if and else blocks.
(Otherwise, actualSleepHours and idealSleepHours would both be undefined when they are being compared in the CalculateSleep function.)

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.