user19161535
user19161535

Reputation:

creating conditional in javascript

I am new to JavaScript and I have created this condition to check if the season is Autumn, Winter, Spring, or Summer.

Here is my JavaScript code:

  1. My question is how to make this in fewer lines of code

  2. How to use both uppercase and lowercase in the same code for eg: October and october

     const 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.")
     }
    

Upvotes: -1

Views: 114

Answers (6)

Shoaib
Shoaib

Reputation: 7

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.")
 }

Upvotes: 0

Mathew Azmat
Mathew Azmat

Reputation: 41

I'm using JavaScript built-in function 'includes' to check whether the string present in an array or not

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

Upvotes: 0

Kiddo
Kiddo

Reputation: 139

Try this with fewer lines of code,

 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()))

Upvotes: 0

otejiri
otejiri

Reputation: 1017

You could assign them as objects and use key value pairs to retrieve

// 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)

Upvotes: 0

Ali Aliyev
Ali Aliyev

Reputation: 163

You can use switch statement. Code is like this:

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;
}

Upvotes: 0

user5117733
user5117733

Reputation:

I like to take the following approach:

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()]}.`)

Upvotes: 1

Related Questions