英文:
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
,但是条件会忽略这一行并直接进入 else
的 console.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('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`)
}
<!-- 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('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`)
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论