卡在尝试使我的条件不区分大小写。

huangapple go评论80阅读模式
英文:

Stuck trying to make my condition not case sensitive

问题

我最近开始学习JavaScript并尝试做一些练习,但遇到了一个问题。

这个条件确实有效,但只有在小写输入提示时才有效,我试图在不区分大小写的情况下进行过滤(如果这样说不太合理,我很抱歉)

let userMonth = prompt('输入月份: ')

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

if (autumn.includes(userMonth.toLowerCase())) {
  console.log(`${userMonth} 在秋季`)

} else if (winter.includes(userMonth.toLowerCase())) {
  console.log(`${userMonth} 在冬季`)

} else if (spring.includes(userMonth.toLowerCase())) {
  console.log(`${userMonth} 在春季`)

} else {
  console.log(`${userMonth} 在夏季`)
}

我尝试使用另一个变量来定义 userMonth,使用 .toLowerCase,但是条件会忽略这一行并直接进入 elseconsole.log 消息。

let userMonth = prompt('输入月份: ')
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} 在秋季`)

} else if (winter.includes(checkMonth)) {
  console.log(`${userMonth} 在冬季`)

} else if (spring.includes(checkMonth)) {
  console.log(`${userMonth} 在春季`)

} else {
  console.log(`${userMonth} 在夏季`)
}
英文:

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

答案1

得分: 3

这是一个函数,即toLowerCase()

let userMonth = prompt('输入月份:')
const checkMonth = userMonth.toLowerCase()

const autumn = ['九月', '十月', '十一月']
const winter = ['十二月', '一月', '二月']
const spring = ['三月', '四月', '五月']
const summer = ['六月', '七月', '八月']

if (autumn.includes(checkMonth)) {
  console.log(`${userMonth} 在秋季`)

} else if (winter.includes(checkMonth)) {
  console.log(`${userMonth} 在冬季`)

} else if (spring.includes(checkMonth)) {
  console.log(`${userMonth} 在春季`)

} else {
  console.log(`${userMonth} 在夏季`)
}
英文:

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

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let userMonth = prompt(&#39;Enter the month: &#39;)
const checkMonth = userMonth.toLowerCase()

const autumn = [&#39;september&#39;, &#39;october&#39;, &#39;november&#39;]
const winter = [&#39;december&#39;, &#39;january&#39;, &#39;february&#39;]
const spring = [&#39;march&#39;, &#39;april&#39;, &#39;may&#39;]
const summer = [&#39;june&#39;, &#39;july&#39;, &#39;august&#39;.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`)
}

<!-- end snippet -->

答案2

得分: 1

不用担心,这只是初学者的错误。最好的人都会犯错。
你可以关注const checkMonth = userMonth.toLowerCase 部分。
在这里,你只是存储了函数的引用。
你必须在toLowerCase()后使用括号,这样它才会返回小写值。

英文:

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.

答案3

得分: 1

let userMonth = prompt('输入月份:')
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} 在秋季`)
} else if (winter.includes(checkMonth)) {
  console.log(`${userMonth} 在冬季`)
} else if (spring.includes(checkMonth)) {
  console.log(`${userMonth} 在春季`)
} else {
  console.log(`${userMonth} 在夏季`)
}
英文:

After prompt you can convert this to lowercase like this [toLowerCase() is a function you are using only toLowerCase] :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let userMonth = prompt(&#39;Enter the month: &#39;)
const checkMonth = userMonth.toLowerCase()

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

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

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月28日 15:32:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350420.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定