Challenge Day 6
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.
Challenge Goals
- Practice nested conditions (
if
,else if
,else
) - Understand odd, even, and multiple concepts
- Add input validation & more human-friendly output
Program Description
- User inputs a number
- Program checks if the number is:
- ✅ Odd
- ✅ Even
- 🎉 Multiple of 10 (shows special message)
- Output includes emojis to make it more expressive 😄
Example Output
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!
Tools & Concepts
prompt()
andparseInt()
to get user inputisNaN()
for input validationif
andelse if
for conditional logic%
(modulus) operator to check odd/even/multiples
Basic Implementation
Initial Version
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 👌");
}
Adding: Multiple of 10
Enhanced Condition
else if (input % 10 === 0 && input % 2 === 0)
Full Code Version 1:
With Special Case
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
Interactive 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 % 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!");
}
}
Addition: Check Negative Numbers
Final Code:
Complete Solution
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!");
}
}