Assignments Of Day 5: Conditional Statements in JavaScript

Rashmi Mishra
0

 Assignments Of Day 5: Conditional Statements in JavaScript

Assignments on Conditional Statements in JavaScript

Here are some assignments along with their solutions based on the concepts of conditional statements (if, else if, else, and switch) in JavaScript.


Assignment 1: Check if a Number is Positive, Negative, or Zero

Problem:
Write a JavaScript program that checks if a number is positive, negative, or zero.

Solution:

javascript

Copy code

let number = prompt("Enter a number:"); // Take input from the user

 

if (number > 0) {

    console.log("The number is positive.");

} else if (number < 0) {

    console.log("The number is negative.");

} else {

    console.log("The number is zero.");

}

  • Explanation:
    • If the number is greater than zero, it prints "The number is positive."
    • If the number is less than zero, it prints "The number is negative."
    • If the number is zero, it prints "The number is zero."

Assignment 2: Check Voting Eligibility Based on Age

Problem:
Write a program that checks if a person is eligible to vote based on their age. The voting age is 18 or older.

Solution:

javascript

Copy code

let age = prompt("Enter your age:"); // Take input from the user

 

if (age >= 18) {

    console.log("You are eligible to vote.");

} else {

    console.log("You are not eligible to vote.");

}

  • Explanation:
    • If the age is 18 or older, it prints "You are eligible to vote."
    • If the age is less than 18, it prints "You are not eligible to vote."

Assignment 3: Categorize the Day of the Week

Problem:
Write a program that prints the name of the day based on a number (1 for Monday, 2 for Tuesday, ..., 7 for Sunday).

Solution:

javascript

Copy code

let day = prompt("Enter a number (1-7) representing the day of the week:"); // Take input from the user

 

switch (day) {

    case '1':

        console.log("Monday");

        break;

    case '2':

        console.log("Tuesday");

        break;

    case '3':

        console.log("Wednesday");

        break;

    case '4':

        console.log("Thursday");

        break;

    case '5':

        console.log("Friday");

        break;

    case '6':

        console.log("Saturday");

        break;

    case '7':

        console.log("Sunday");

        break;

    default:

        console.log("Invalid input! Please enter a number between 1 and 7.");

}

  • Explanation:
    • The program checks the value of day and prints the corresponding day of the week.
    • If the input is not a valid number (1-7), it prints "Invalid input! Please enter a number between 1 and 7."

Assignment 4: Grade Classification Based on Marks

Problem:
Write a program that classifies a student’s grade based on their marks. Use the following classification:

  • 90 and above: A
  • 75-89: B
  • 50-74: C
  • Below 50: Fail

Solution:

javascript

Copy code

let marks = prompt("Enter your marks (0-100):"); // Take input from the user

 

if (marks >= 90) {

    console.log("Grade A");

} else if (marks >= 75) {

    console.log("Grade B");

} else if (marks >= 50) {

    console.log("Grade C");

} else {

    console.log("Fail");

}

  • Explanation:
    • The program checks the marks against multiple conditions.
    • If the marks are 90 or above, it prints "Grade A."
    • If the marks are between 75 and 89, it prints "Grade B."
    • If the marks are between 50 and 74, it prints "Grade C."
    • If the marks are below 50, it prints "Fail."

Assignment 5: Find Largest of Three Numbers

Problem:
Write a program that finds the largest of three numbers.

Solution:

javascript

Copy code

let num1 = prompt("Enter first number:");

let num2 = prompt("Enter second number:");

let num3 = prompt("Enter third number:");

 

if (num1 >= num2 && num1 >= num3) {

    console.log("The largest number is " + num1);

} else if (num2 >= num1 && num2 >= num3) {

    console.log("The largest number is " + num2);

} else {

    console.log("The largest number is " + num3);

}

  • Explanation:
    • The program checks which number is the largest by comparing all three numbers using the >= operator.
    • It then prints the largest number.

Assignment 6: Check Leap Year

Problem:
Write a program that checks if a given year is a leap year. A year is a leap year if:

  • It is divisible by 4.
  • But, if it is divisible by 100, it must also be divisible by 400.

Solution:

javascript

Copy code

let year = prompt("Enter a year:");

 

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {

    console.log(year + " is a leap year.");

} else {

    console.log(year + " is not a leap year.");

}

  • Explanation:
    • The program first checks if the year is divisible by 4 and not divisible by 100, or divisible by 400. If either of these conditions is true, it is a leap year.
    • Otherwise, it is not a leap year.

Assignment 7: Determine the Age Group

Problem:
Write a program that determines the age group of a person based on their age. The age groups are:

  • 0-12: Child
  • 13-19: Teenager
  • 20-39: Adult
  • 40 and above: Senior Citizen

Solution:

javascript

Copy code

let age = prompt("Enter your age:");

 

if (age >= 0 && age <= 12) {

    console.log("You are a Child.");

} else if (age >= 13 && age <= 19) {

    console.log("You are a Teenager.");

} else if (age >= 20 && age <= 39) {

    console.log("You are an Adult.");

} else if (age >= 40) {

    console.log("You are a Senior Citizen.");

} else {

    console.log("Invalid age.");

}

  • Explanation:
    • The program checks the age and classifies it into the correct age group.
    • It uses multiple else if statements to match the given age against different ranges.

Conclusion:

These assignments will help reinforce the understanding of how conditional statements like if, else if, else, and switch work in JavaScript. You can practice these examples and modify them to create your own decision-making programs.

4o mini

You said:

more

ChatGPT said:

ChatGPT

Sure! Here are additional assignments based on conditional statements (if, else if, else, and switch) in JavaScript, along with their solutions.


Assignment 8: Check if a Year is a Century Year

Problem:
Write a program that checks if a given year is a century year. A century year is divisible by 100, but not necessarily a leap year.

Solution:

javascript

Copy code

let year = prompt("Enter a year:");

 

if (year % 100 == 0) {

    console.log(year + " is a century year.");

} else {

    console.log(year + " is not a century year.");

}

  • Explanation:
    • The program checks if the year is divisible by 100. If true, it prints that the year is a century year; otherwise, it states it's not.

Assignment 9: Check If a Number is Divisible by Both 3 and 5

Problem:
Write a program that checks if a number is divisible by both 3 and 5.

Solution:

javascript

Copy code

let number = prompt("Enter a number:");

 

if (number % 3 === 0 && number % 5 === 0) {

    console.log("The number is divisible by both 3 and 5.");

} else {

    console.log("The number is not divisible by both 3 and 5.");

}

  • Explanation:
    • The program checks if the number is divisible by both 3 and 5 using the logical && operator.
    • If both conditions are true, it prints the appropriate message.

Assignment 10: Check Grade Based on Score Using Switch Statement

Problem:
Write a program that assigns grades based on the score:

  • 90 and above: A
  • 80-89: B
  • 70-79: C
  • 60-69: D
  • Below 60: F

Use the switch statement.

Solution:

javascript

Copy code

let score = prompt("Enter your score:");

 

switch (true) {

    case (score >= 90):

        console.log("Grade A");

        break;

    case (score >= 80):

        console.log("Grade B");

        break;

    case (score >= 70):

        console.log("Grade C");

        break;

    case (score >= 60):

        console.log("Grade D");

        break;

    default:

        console.log("Grade F");

}

  • Explanation:
    • The program uses the switch(true) statement to check different ranges of scores. The conditions are evaluated in sequence, and the appropriate grade is assigned based on the score.

Assignment 11: Find the Smallest of Three Numbers

Problem:
Write a program that finds the smallest of three numbers.

Solution:

javascript

Copy code

let num1 = prompt("Enter first number:");

let num2 = prompt("Enter second number:");

let num3 = prompt("Enter third number:");

 

if (num1 <= num2 && num1 <= num3) {

    console.log("The smallest number is " + num1);

} else if (num2 <= num1 && num2 <= num3) {

    console.log("The smallest number is " + num2);

} else {

    console.log("The smallest number is " + num3);

}

  • Explanation:
    • The program checks all three numbers and compares them using <= to determine the smallest one.
    • It prints the smallest number after evaluating all the conditions.

Assignment 12: Check If a Character is a Vowel or Consonant

Problem:
Write a program that checks whether a character entered by the user is a vowel (a, e, i, o, u) or a consonant.

Solution:

javascript

Copy code

let char = prompt("Enter a character:");

 

if (char.length === 1 && /[a-zA-Z]/.test(char)) {

    if ('aeiouAEIOU'.includes(char)) {

        console.log(char + " is a vowel.");

    } else {

        console.log(char + " is a consonant.");

    }

} else {

    console.log("Please enter a valid single alphabetic character.");

}

  • Explanation:
    • The program first checks if the user has entered a single alphabetic character.
    • Then, it checks if the character is a vowel by looking if it is included in the string "aeiouAEIOU". If true, it prints that the character is a vowel; otherwise, it prints that it's a consonant.

Assignment 13: Determine the Quadrant of a Point

Problem:
Write a program that determines the quadrant of a point in a 2D plane, given the x and y coordinates.

  • Quadrant 1: x > 0, y > 0
  • Quadrant 2: x < 0, y > 0
  • Quadrant 3: x < 0, y < 0
  • Quadrant 4: x > 0, y < 0
  • If either x or y is 0, the point lies on one of the axes.

Solution:

javascript

Copy code

let x = prompt("Enter the x-coordinate:");

let y = prompt("Enter the y-coordinate:");

 

if (x > 0 && y > 0) {

    console.log("Point lies in Quadrant 1");

} else if (x < 0 && y > 0) {

    console.log("Point lies in Quadrant 2");

} else if (x < 0 && y < 0) {

    console.log("Point lies in Quadrant 3");

} else if (x > 0 && y < 0) {

    console.log("Point lies in Quadrant 4");

} else if (x === '0' && y === '0') {

    console.log("Point lies at the origin.");

} else if (x === '0') {

    console.log("Point lies on the y-axis.");

} else if (y === '0') {

    console.log("Point lies on the x-axis.");

} else {

    console.log("Invalid input.");

}

  • Explanation:
    • The program checks the values of x and y and determines which quadrant the point lies in, or if the point is on one of the axes or at the origin.

Assignment 14: Display the Largest Number from a List

Problem:
Write a program that takes a list of numbers from the user and determines the largest number in the list.

Solution:

javascript

Copy code

let numbers = prompt("Enter a list of numbers separated by commas:").split(",");

let largest = Number(numbers[0]);

 

for (let i = 1; i < numbers.length; i++) {

    if (Number(numbers[i]) > largest) {

        largest = Number(numbers[i]);

    }

}

 

console.log("The largest number is: " + largest);

  • Explanation:
    • The program prompts the user to enter numbers separated by commas. It then splits the input string into an array of numbers.
    • It compares each number in the list and keeps track of the largest number.

Assignment 15: Determine the Type of Triangle Based on Side Lengths

Problem:
Write a program that determines the type of triangle based on its three side lengths. The types are:

  • Equilateral (all sides are equal)
  • Isosceles (two sides are equal)
  • Scalene (no sides are equal)

Solution:

javascript

Copy code

let side1 = prompt("Enter the first side length:");

let side2 = prompt("Enter the second side length:");

let side3 = prompt("Enter the third side length:");

 

if (side1 === side2 && side2 === side3) {

    console.log("The triangle is Equilateral.");

} else if (side1 === side2 || side2 === side3 || side1 === side3) {

    console.log("The triangle is Isosceles.");

} else {

    console.log("The triangle is Scalene.");

}

  • Explanation:
    • The program checks the side lengths and determines if the triangle is equilateral, isosceles, or scalene based on the equality of the sides.


Post a Comment

0Comments

Post a Comment (0)