Exercise 1: Check if a Number is Positive — ⭐☆☆ (Easy)
Write a function that checks whether a number is positive, negative, or
zero.
function checkNumber(num) {
if (num > 0) {
return "Positive";
} else if (num < 0) {
return "Negative";
} else {
return "Zero";
}
}
Exercise 2: Even or Odd Number Checker — ⭐☆☆ (Easy)
Write a function that determines if a given number is even or odd.
function isEvenOrOdd(num) {
if (num % 2 === 0) {
return "Even";
} else {
return "Odd";
}
}
Exercise 3: Determine if a Person is Eligible to Vote — ⭐☆☆ (Easy)
Write a function that checks if a person is eligible to vote based on
age. A person is eligible if their age is 18 or above.
function canVote(age) {
if (age >= 18) {
return "Eligible to vote";
} else {
return "Not eligible to vote";
}
}
Exercise 4: Grade Calculator — ⭐⭐☆ (Medium)
Create a function that calculates a letter grade based on a numerical
score:
90-100: 'A'
80-89: 'B'
70-79: 'C'
60-69: 'D'
Below 60: 'F'
Implement this using both if/else if and
switch statements.
// If/else if version
function calculateGradeIfElse(score) {
if (score >= 90 && score <= 100) return 'A';
else if (score >= 80) return 'B';
else if (score >= 70) return 'C';
else if (score >= 60) return 'D';
else if (score >= 0) return 'F';
else return 'Invalid score';
}
// Switch version
function calculateGradeSwitch(score) {
const gradeRange = Math.floor(score / 10);
switch (gradeRange) {
case 10:
case 9: return 'A';
case 8: return 'B';
case 7: return 'C';
case 6: return 'D';
case 5:
case 4:
case 3:
case 2:
case 1:
case 0: return 'F';
default: return 'Invalid score';
}
}
Exercise 5: Day of Week Message Generator — ⭐⭐☆ (Medium)
Create a function that takes a day number (1-7) and returns a custom
message for each day of the week:
1: Monday – "Start of the work week"
2: Tuesday – "Taco Tuesday!"
3: Wednesday – "Halfway there!"
4: Thursday – "Almost the weekend"
5: Friday – "TGIF!"
6: Saturday – "Weekend fun day"
7: Sunday – "Rest day"
Implement this using both if/else if and
switch statements.
// If/else if version
function getDayMessageIfElse(dayNumber) {
if (dayNumber === 1) return "Start of the work week";
else if (dayNumber === 2) return "Taco Tuesday!";
else if (dayNumber === 3) return "Halfway there!";
else if (dayNumber === 4) return "Almost the weekend";
else if (dayNumber === 5) return "TGIF!";
else if (dayNumber === 6) return "Weekend fun day";
else if (dayNumber === 7) return "Rest day";
else return "Invalid day number";
}
// Switch version
function getDayMessageSwitch(dayNumber) {
switch (dayNumber) {
case 1: return "Start of the work week";
case 2: return "Taco Tuesday!";
case 3: return "Halfway there!";
case 4: return "Almost the weekend";
case 5: return "TGIF!";
case 6: return "Weekend fun day";
case 7: return "Rest day";
default: return "Invalid day number";
}
}