Day-4 : 30daysofJavascript

·

5 min read

Day 4 is all about Conditional statements in JS, If else cases, switch case, and other ternary operators. I have solved these exercises from here. These Blogs are for my accountability. You are awesome, and showing up everyday is a skill, called consistency. Lets GO!

  1. Get user input using prompt(“Enter your age:”). If user is 18 or older , give feedback:'You are old enough to drive' but if not 18 give another feedback stating to wait for the number of years he needs to turn 18.

Enter your age: 30 You are old enough to drive.

Enter your age:15 You are left with 3 years to drive.

var question1 = prompt("Enter your Age", "Correct Age!!!!!!!!!!!")
var checkAgeGreaterthan18 = 18-Number(question1);
var feedback = parseInt(question1)>=18?"You are old enough to drive":"Wait for ${checkAgeGreaterthan18} years"

Follow-up questions

  • Compare the values of myAge and yourAge using if … else. Based on the comparison and log the result to console stating who is older (me or you). Use prompt(“Enter your age:”) to get the age as input.

  •       var myAge = 23;
          if(question1>=myAge){
          console.log("Bhaii - Bhaii")
          }
          else{
          console.log("I am older than you!");
          }
    
  • If a is greater than b return 'a is greater than b' else 'a is less than b'. Try to implement it in to ways

    • using if else

    • ternary operator.

const a = 24;
const b = 90;
if(a>b) console.log("a is greater than b")
else console.log("a is less than b")

//using ternary operator
const answer = a>b?"a,${a} is greater than b ${b}":"a${a} is less than b${b}"
console.log(answer);
    • Even numbers are divisible by 2 and the remainder is zero. How do you check, if a number is even or not using JavaScript?
  •       let number = prompt("Enter a number");
          const checkEven = number%2===0?"Even":"Odd"
    
    • Write a code which can give grades to students according to theirs scores:

      * 80-100, A

      * 70-89, B

      * 60-69, C

      * 50-59, D

      * 0-49, F

      * javascript let num = prompt('Enter Score'); switch (true) { case num >= 80: console.log('Grade A'); break; case num >= 70: console.log('Grade B'); break; case num >= 60: console.log('Grade C'); break; default: console.log('Entered value was not a number'); }

  1. Check if the season is Autumn, Winter, Spring or Summer. If the user input is :

  2. September, October or November, the season is Autumn.

  3. December, January or February, the season is Winter.

  4. March, April or May, the season is Spring

  5. June, July or August, the season is Summer

let season = prompt("Enter Season"," January, February)");
let seasonInLowerCase = season.toLowerCase();
switch(seasonInLowerCase){
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;

    default: console.log("Enter input correctly")
}
  1. Check if a day is weekend day or a working day. Your script will take day as an input.
let todaysDay = prompt("What is the day  today?");
let todayInLowerCase = todaysDay.toLowerCase();
switch(todayInLowerCase){
case 'saturday': 
    console.log("Saturday is a weekend.");
break;

case 'sunday': 
    console.log("Sunday is a weekend.");
break;
case 'monday':
    console.log("Monday is a working day.");
break;
case 'tuesday':
    console.log("Tuesday is a working day.");
break;
case 'wednesday':
    console.log("Wednesday is a working day.");
break;
case 'thursday':
    console.log("Thursday is a working day.");
break;
case 'friday':
    console.log("Friday is a working day.");
break;

default: console.log("Enter correct week name")
}
/*
Output
What is the day  today? Saturday
  Saturday is a weekend.

  What is the day today? saturDaY
  Saturday is a weekend.

  What is the day today? Friday
  Friday is a working day.

  What is the day today? FrIDAy
  Friday is a working day.
*/
  1. Write a program which tells the number of days in a month.

  2.   let Month = prompt("Enter a month:").toLowerCase();
      switch(Month){
          case 'january':
          console.log("January has 31 days.")
      break;
          case 'february':
          console.log("February has 28 days.")
      break;
          case 'march':
          console.log("March has 31 days.")
      break;
          case 'april':
          console.log("April has 30 days.")
      break;
          case 'may':
          console.log("May has 31 days.")
      break;
          case 'june':
          console.log("June has 30 days.")
      break;
          case 'july':
          console.log("July has 31 days.")
      break;
          case 'august':
          console.log("August has 31 days.")
      break;
          case 'september':
          console.log("September has 30 days.")
      break;
          case 'october':
          console.log("October has 31 days.")
      break;
          case 'november':
          console.log("November has 30 days.")
      break;
          case 'december':
          console.log("December has 31 days.")
      break;
      }
    
    1. Write a program which tells the number of days in a month, now consider leap year.

       let year = prompt("Enter year");
       function isLeapYear(year) {
           return (year % 400 === 0) || (year % 4 === 0 && year % 100 !== 0);
       }
       if(isLeapYear(year)===true){
        let Month = prompt("Enter a month:").toLowerCase();
        switch(Month){
            case 'january':
            console.log("January has 31 days.")
        break;
            case 'february':
            console.log("February has 29 days.")
        break;
            case 'march':
            console.log("March has 31 days.")
        break;
            case 'april':
            console.log("April has 30 days.")
        break;
            case 'may':
            console.log("May has 31 days.")
        break;
            case 'june':
            console.log("June has 30 days.")
        break;
            case 'july':
            console.log("July has 31 days.")
        break;
            case 'august':
            console.log("August has 31 days.")
        break;
            case 'september':
            console.log("September has 30 days.")
        break;
            case 'october':
            console.log("October has 31 days.")
        break;
            case 'november':
            console.log("November has 30 days.")
        break;
            case 'december':
            console.log("December has 31 days.")
        break;
        }
       }
       else{
        let Month = prompt("Enter a month:").toLowerCase();
        switch(Month){
            case 'january':
            console.log("January has 31 days.")
        break;
            case 'february':
            console.log("February has 28 days.")
        break;
            case 'march':
            console.log("March has 31 days.")
        break;
            case 'april':
            console.log("April has 30 days.")
        break;
            case 'may':
            console.log("May has 31 days.")
        break;
            case 'june':
            console.log("June has 30 days.")
        break;
            case 'july':
            console.log("July has 31 days.")
        break;
            case 'august':
            console.log("August has 31 days.")
        break;
            case 'september':
            console.log("September has 30 days.")
        break;
            case 'october':
            console.log("October has 31 days.")
        break;
            case 'november':
            console.log("November has 30 days.")
        break;
            case 'december':
            console.log("December has 31 days.")
        break;
        }
       }