skip to Main Content

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

    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`)
    }

3

Answers


  1. It is supposed to be a function, i.e., toLowerCase()

    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`)
    }
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. After prompt you can convert this to lowercase like this [toLowerCase() is a function you are using only toLowerCase] :

    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']
    
    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`)
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search