What am I doing wrong with .toLowerCase?

Hi all,

I’m practicing the JavaScript syntax in the following exercise: JavaScript practice

The exercise is asking me to:

  • Create a function colorMessage() that takes 2 string arguments, favoriteColor and shirtColor .
  • If the value of favoriteColor is the same as the value of shirtColor return the string 'The shirt is your favorite color!' .
  • If not, return the string 'That is a nice color.'

So I thought I would use the .toLowerCase() method in order to prevent having the same color compared as a different one, if one of them had the first letter capitalized.

However, when I tested it, it identified ‘red’ and ‘Red’ as different colors. Do someone notice where is my mistake?

My code:

const colorMessage = (favoriteColor, shirtColor) => {
  favoriteColor.toLowerCase();
  shirtColor.toLowerCase();
  if (favoriteColor === shirtColor) {
    return "The shirt is your favorite color!";
  } else {
    return "That is a nice color.";
  }
}

console.log(colorMessage("red", "Red"));

image

If you put the console into your function and log favoriteColor and shirtColor below the lines where you use the method .toLowerCase() , you will see, that the arguments stay unchanged. You have to reassign them.

3 Likes

@mirja_t is correct, although if you’re new to coding what that means may not be clear I expect.

As an example, in line 4 you use the method shirtColor.toLowerCase(). That will indeed change ‘Red’ to ‘red’, but it’s not going to put it into the variable shirt Color — it’s just going be lost.

To reassign it, I think you’ll need to change line 4 to shirtColor = shirtColor.toLowerCase(), which should put the result of toLowerCase back into shirtColor.

I hope that helps.

2 Likes

Thank you both @mirja_t @christotainteddingo :smile:. Now I get it. Basically, the change is “lost” as I don’t reassign the value of the variables. I´m new indeed hahaha

2 Likes