So I went back to the start, because JavaScript confuses me a lot. I like the usage of arrays because you can compile a lot of data into nearly singular values, or just a heavy stack of collated data that becomes maybe one or two, or three or four variables; but all at once.
I had a good friend of mine help me navigate through this, and all of the notation was mostly their commentary on how to re-do things:
/* const monday = 4.5;
const tuesday = 8;
const wednesday = 6;
const thursday = 6;
const friday = 4;
const saturday = 10;
const sunday = 12;
const weekDays = (monday, tuesday, wednesday, thursday, friday);
const weekEnds = (saturday, sunday);
*/ //all of the above, chunky, clunky, convoluted
const weekDays = ['monday','tuesday', 'wednesday', 'thursday', 'friday'];
const weekEnds = ['saturday', 'sunday']; //string literals to get around the ReferenceError, a pre-definition of sorts instead of assigning variables previous to the array with mutliple const-variables like the above
const HoursSlept = [4.5, 8, 6, 6.5, 4];
const weekendHoursSlept = [10, 12];
const getSleepHours = (day) => {
if (weekDays.includes(day)) {
return Math.floor(Math.random() * (8 - 4 + 1)) + 4;
} else if (weekEnds.includes(day)) {
return Math.floor(Math.random() * (14 - 8 + 1)) + 8;
} else {
return 0;
}
};
// console.log(getSleepHours(tuesday)); tested/works
// console.log(getSleepHours(saturday)); tested/works
const getActualSleepHours = () => {
let totalHoursSlept = 0;
for (let i = 0; i < weekDays.length; i++) {
totalHoursSlept += getSleepHours(weekDays[i]);
}
for (let i = 0; i < weekEnds.length; i++) {
totalHoursSlept += getSleepHours(weekEnds[i]);
}
return totalHoursSlept;
};
// console.log(totalHoursSlept(weekDays)); // totalHoursSlept is not defined here
// console.log(getActualSleepHours()); // tested/works
const getIdealSleepHours = () => {
const idealHours = 8;
return (idealHours * 7);
};
// console.log(getIdealSleepHours()); //tested/works
const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();
const sleepHourDifferential = Math.abs(actualSleepHours - idealSleepHours);
if (actualSleepHours === idealSleepHours) {
return console.log('Weekly sleep goal met.');
} else if (actualSleepHours < idealSleepHours) {
return console.log(`Weekly sleep goal short: ${sleepHourDifferential} hour(s).`);
} else {
return console.log(`Weekly sleep goals met and exceeded ${sleepHourDifferential} hour(s).`)
}
};
// console.log(calculateSleepDebt()); // this is erroneous; as console.log simply logs the result of 'calling' the function `calculateSleepDebt`, as opposed to the action of 'calling' the function of `calculateSleepDebt` itself and thus accessing its numerous installed values and functions which produce:
// call the function simply shown below
calculateSleepDebt();
I’m not sure if this is functional, because I produced many errors. All the notes I wrote down into the code so it looks really junky but I wanted to be prepared. I know this is considered…maybe backtracking, because I’m re-doing preliminary coursework with knowledge that exceeds the current curriculum at the point-in-time where you are supposed to be mostly working with just simple arrow functions and the like. I just didn’t know how else to achieve the intended results without my friend pointing to other avenues inside JavaScript.
Thanks for raising this question in the community. I have stated some feedback below, hope they are of help to your growth and knowledge referencing. Feel free to ask if you have doubts.
I supposed these two constant variables will be used later in your code?
Although I understand this may be a practice only but having a function just to return consistent values with no changes may be a little exaggerated hence, I believe getIdealSleepHours function may not be necessary.
Do consider placing them with other constants such as the following section of your code so it looks neat.
The reason why you used this because you just want to generate a random hour I supposed?
Then I think this is alright. Otherwise, you may want to think if this is consistent.
I believe why this is erroneous because all your return statements are returning another “console.log” with your string statements too.
OR
Do you mean calling ‘calculateSleepDebt’ and ‘calculateSleepDebt( )’ this is different? And yes, they are because the former involves passing the function as an object instead of invoking the function (meaning whatever instructions that is declared inside the function will be executed). I have stated a simple example below after this code, you can try to refer see if it makes sense.
if (actualSleepHours === idealSleepHours) {
return 'Weekly sleep goal met.';
} else if (actualSleepHours < idealSleepHours) {
return `Weekly sleep goal short: ${sleepHourDifferential} hour(s).`;
} else {
return `Weekly sleep goals met and exceeded ${sleepHourDifferential} hour(s).`;
}