toLowerCase help

Hello, everyone. I need some help regarding .toLowerCase() and how to implement it into the following code. As of right now If I:

console.log(getSleepHours(‘Monday’)); I will get the correct 8 displayed in the log. however if I instead type:
console.log(getSleepHours(‘monday’)); I will not get the correct answer. I know it’s silly but I would like to know for future reference the best way to get the correct log displayed with capital or lowercase typed in.

Thank you,
WT

function getSleepHours(day) {
let result = 1;

switch (day) {
case ‘Monday’:
result = 8;
break;
case ‘Tuesday’:
result = 7;
break;
case ‘Wednesday’:
result = 8;
break;
case ‘Thursday’:
result = 8;
break;
case ‘Friday’:
result = 8;
break;
case ‘Saturday’:
result = 6;
break;
case “Sunday”:
result = 6;
break;
}

return result;

};

console.log(getSleepHours(‘Monday’));

Just reassign the variable from the function parameter and compare it with the day set to lower case manually in the switch case like this:

function getSleepHours(day) {
day = day.toLowerCase();
let result;
switch (day) {
case 'monday':
result = 8;
break;
}
...
return result;
}

Then you get the correct result whether you pass the day to the function with a capital letter or not…

1 Like

May I know what you’re trying to achieve using .toLowerCase() method?

Meaning of .toLowerCase() method:
→ The toLowerCase() method will convert a string value to lower case.

For example:

const str = "Daddy named Alex";

console.log(str.toLowerCase(str));
// Prints daddy named alex

Mostly I wanted to console.log(getSleepHours(‘Wednesday’)); and get the same result as console.log(getSleepHours(‘wednesday’));

I know there is little point in doing it in this exercise, but I just wanted to try to understand how to do it now.

Thank you very much. Have a nice day.

1 Like