Function Mastery — Solutions
This page contains the official solutions for Module 4.
If you haven’t tried the challenges yet — stop scrolling.
“Copying solutions doesn’t make you better. Understanding why they work does.”
🔥 Challenge 1 — Even or Odd Function
Create a function that:
- Receives a number
- Returns Even or Odd
JavaScript
function checkEvenOdd(number) {
if (number % 2 === 0) {
return "Even";
} else {
return "Odd";
}
}
console.log(checkEvenOdd(7));
console.log(checkEvenOdd(10));
Modulo (%) is the core logic here.
Same weapon, different use case.
🔥 Challenge 2 — Grade Generator
Rules:
- Input score
- Return grade (A–E)
JavaScript
function getGrade(score) {
if (score >= 90) return "A";
if (score >= 80) return "B";
if (score >= 70) return "C";
if (score >= 60) return "D";
return "E";
}
console.log(getGrade(92));
console.log(getGrade(76));
console.log(getGrade(60));
Early return keeps logic clean and readable. No nested mess.
🔥 Challenge 3 — Login Function
Conditions:
- Username must match
- Password must match
- Return success or error message
JavaScript
function login(username, password) {
const correctUsername = "admin";
const correctPassword = "12345";
if (username === correctUsername && password === correctPassword) {
return "Login successful 👋";
} else {
return "Invalid credentials ❌";
}
}
console.log(login("admin", "12345"));
console.log(login("user", "123"));
This is simplified authentication logic. Same concept used in real apps.
🚀 Bonus — Cleaner Validation
More detailed feedback:
JavaScript
function login(username, password) {
if (username !== "admin") {
return "Username not found";
} else if (password !== "12345") {
return "Wrong password";
} else {
return "Login successful";
}
}
Same logic. Better UX.
