Odd, Even, or Special?
Today's challenge focuses on number classification with layered logic to sharpen coding reasoning skills, including checking for odd, even, and multiples of 10.
The goal of this challenge was to classify numbers as odd, even, or special cases such as multiples of 10 while practicing conditional logic.
Enter a number: 27
➡️ 27 is an odd number 🔹
Enter a number: 44
➡️ 44 is an even number 🔸
Enter a number: 30
🎉 30 is an even number & multiple of 10!Challenge Goals
This challenge focuses on nested conditions, number classification, input validation, and creating more user-friendly outputs.
Basic Implementation
const input = parseInt(prompt("Enter a number:"));
if (isNaN(input)) {
console.log("Please enter a valid number 😅");
} else if (input % 2 === 0) {
console.log("This is an even number 👍");
} else {
console.log("This is an odd number 👌");
}This version can already distinguish between odd and even numbers, but there is still room for improvement.
Adding a Special Case
Multiples of 10 should be checked before the general even-number condition because every multiple of 10 is already an even number.
else if (input % 10 === 0 && input % 2 === 0)Version With Special Logic
const input = parseInt(prompt("Enter a number:"));
if (isNaN(input)) {
console.log("Please enter a valid number 😅");
} else if (input % 10 === 0 && input % 2 === 0) {
console.log("🎉 This is an even number and multiple of 10!");
} else if (input % 2 === 0) {
console.log("🔸 This is an even number!");
} else {
console.log("🔹 This is an odd number!");
}Bonus Challenge: Loop Version
To make the program more interactive, I added a loop that keeps asking for input until the user decides to quit.
let input;
while (true) {
input = prompt("Enter a number (type 'exit' to quit)");
if (input === "exit") {
console.log("Goodbye! 👋");
break;
}
const number = parseFloat(input);
if (isNaN(number)) {
console.log("❌ Please enter a valid number 😅");
} else if (number % 10 === 0 && number % 2 === 0) {
console.log("🎊 This is an even number and multiple of 10!");
} else if (number % 2 === 0) {
console.log("🔸 This is an even number!");
} else {
console.log("🔹 This is an odd number!");
}
}Final Version
let input;
while (true) {
input = prompt("Enter a number (type 'exit' to quit)");
if (input === "exit") {
console.log("Goodbye! 👋");
break;
}
const number = parseFloat(input);
if (isNaN(number)) {
console.log("❌ Please enter a valid number 😅");
} else if (number < 0) {
console.log("📉 This is a negative number...");
} else if (number % 10 === 0 && number % 2 === 0) {
console.log("🎊 This is an even number and multiple of 10!");
} else if (number % 2 === 0) {
console.log("🔸 This is an even number!");
} else {
console.log("🔹 This is an odd number!");
}
}Output: Even number & multiple of 10 🎉
