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.
Am I doing this haphazardly and wrong?