Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Stuck trying to make my condition not case sensitive

I’ve have recently started to pick up JavaScript and trying to do some exercises and ran into a problem.

This condition does work but only when the prompt is entered in lowercases and I’m trying to filter it down without being case sensitive (sorry if it doesn’t make much sense)

    let userMonth = prompt('Enter the month: ')

    const autumn = ['september', 'october', 'november']
    const winter = ['december', 'january', 'february']
    const spring = ['march', 'april', 'may']
    const summer = ['june', 'july', 'august']

    if(autumn.includes(userMonth)){
      console.log(`${userMonth} is in Autumn`)

    } else if (winter.includes(userMonth)){
      console.log(`${userMonth} is in Winter`)

    } else if (spring.includes(userMonth)){
      console.log(`${userMonth} is in Spring`)

    } else {
      console.log(`${userMonth} is in Summer`)
    }

I tried writing another variable to define userMonth with .toLowerCase but the condition ignores the line and goes straight to the else console.log message

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    let userMonth = prompt('Enter the month: ')
    const checkMonth = userMonth.toLowerCase

    const autumn = ['september', 'october', 'november']
    const winter = ['december', 'january', 'february']
    const spring = ['march', 'april', 'may']
    const summer = ['june', 'july', 'august'.t]

    if(autumn.includes(checkMonth)){
      console.log(`${userMonth} is in Autumn`)

    } else if (winter.includes(checkMonth)){
      console.log(`${userMonth} is in Winter`)

    } else if (spring.includes(checkMonth)){
      console.log(`${userMonth} is in Spring`)

    } else {
      console.log(`${userMonth} is in Summer`)
    }

>Solution :

Ok, You don’t have to worry about it. Just a beginner’s mistake. Happens to the best of us.
Can you focus on the const checkMonth = userMonth.toLowerCase section.
Here you have just storing the reference of the function.
You have to use the parenthesis after the toLowerCase() so that it will return the lowercase value.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading