我是 JavaScript 新手,我創建了這個條件來檢查季節是Autumn、Winter、Spring還是Summer。
這是我的 JavaScript 代碼:
我的問題是如何在更少的代碼行中做到這一點
如何在同一代碼中同時使用大寫和小寫,例如:
October和octoberconst seasonName = prompt('entere the month name') if (seasonName == 'september') { console.log("The Season is Autumn.") } else if (seasonName == 'october') { console.log("The Season is Autumn.") } else if (seasonName == 'november') { console.log("The Season is Autumn.") } else if (seasonName == 'december') { console.log("The Season is Winter.") } else if (seasonName == 'january') { console.log("The Season is Winter.") } else if (seasonName == 'february') { console.log("The Season is Winter.") } else if (seasonName == 'march') { console.log("The Season is Winter.") } else if (seasonName == 'april') { console.log("The Season is Spring.") } else if (seasonName == 'may') { console.log("The Season is Spring.") } else if (seasonName == 'june') { console.log("The Season is Summer.") } else if (seasonName == 'july') { console.log("The Season is Summer.") } else if (seasonName == 'august') { console.log("The Season is Summer.") }
uj5u.com熱心網友回復:
我喜歡采用以下方法:
const seasonsByMonth = {
'january': 'winter',
'february': 'winter',
' march': 'winter',
'april': 'spring',
...
}
const seasonName = prompt('entere the month name');
console.log(`The Season is ${seasonsByMonth[seasonName.toLowerCase()]}.`)
uj5u.com熱心網友回復:
你可以使用
switch陳述句。代碼是這樣的:
switch(seasonName) {
case "september":
case "october":
case "november":
console.log("The Season is Autumn.");
break;
case "december":
case "january":
case "february":
console.log("The Season is Winter.");
break;
case "march":
case "april":
case "may":
console.log("The Season is Spring.");
break;
case "june":
case "july":
case "august":
console.log("The Season is Summer.");
break;
}
uj5u.com熱心網友回復:
您可以將它們分配為物件并使用鍵值對來檢索
// add your months as objects
const seasonObj = {
Autumn: ["september", "october", "november", ],
Spring: ["april", "may"],
summer: ["june", "july", "august"],
Winter: ["december", "january", "february", "march"]
}
function getSeason(value) {
for (const [key, val] of Object.entries(seasonObj)) {
if(val.includes(value))
{
return `the Season is ${key}`
}
}
}
const result = getSeason("april");
console.log(result)
uj5u.com熱心網友回復:
用更少的代碼行試試這個,
const seasonName = prompt('entere the month name')
let obj = {
'summer': ['august', 'july', 'june'],
'spring': ['may', 'april'],
'winder': ['march', 'february', 'january'],
'autumn': ['september', 'october', 'november']
}
function findSeason(seasonName) {
let result = 'Invalid Month Name'
let keys = Object.keys(obj)
for (let i = 0; i < keys.length; i ) {
for (let j = 0; j < obj[keys[i]].length; j ) {
if (seasonName === obj[keys[i]][j]) {
result = `The Season is ${keys[i]}`
}
}
}
return result
}
console.log(findSeason(seasonName.toLowerCase()))
uj5u.com熱心網友回復:
我正在使用 JavaScript 內置函式“包含”來檢查字串是否存在于陣列中
const winterMonthName = ['January', 'February', 'December', 'january', 'february', , 'december'];
const springMonthName = ['March', 'April', 'May', 'march', 'april', 'may']
const summerMonthName = ['June', 'July', 'August', 'june', 'july', 'august']
const autumnMonthName = ['September', 'October', 'November', 'september', 'october', 'november']
const type = 'July';
if(winterMonthName.includes(type)){
console.log('The Season is Winter')
}
else if(springMonthName.includes(type)){
console.log('The Season is Spring')
}
else if(summerMonthName.includes(type)){
console.log('The Season is Summer')
}
else if(autumnMonthName.includes(type)){
console.log('The Season is Winter')
}
else {
console.log('You entered wrong')
}
uj5u.com熱心網友回復:
const seasonName = prompt('entere the month name')
const seasonNameLowercase = seasonName.toLowerCase();
if (seasonNameLowercase == 'september') {
console.log("The Season is Autumn.")
}
else if (seasonNameLowercase == 'october') {
console.log("The Season is Autumn.")
}
else if (seasonNameLowercase == 'november') {
console.log("The Season is Autumn.")
}
else if (seasonNameLowercase == 'december') {
console.log("The Season is Winter.")
}
else if (seasonNameLowercase == 'january') {
console.log("The Season is Winter.")
}
else if (seasonNameLowercase == 'february') {
console.log("The Season is Winter.")
}
else if (seasonNameLowercase == 'march') {
console.log("The Season is Winter.")
}
else if (seasonNameLowercase == 'april') {
console.log("The Season is Spring.")
}
else if (seasonNameLowercase == 'may') {
console.log("The Season is Spring.")
}
else if (seasonNameLowercase == 'june') {
console.log("The Season is Summer.")
}
else if (seasonNameLowercase == 'july') {
console.log("The Season is Summer.")
}
else if (seasonNameLowercase == 'august') {
console.log("The Season is Summer.")
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/512677.html
