Conditionals in Javascript by Ctrl-Zombies
Conditionals homework in Javascript
Nested Conditionals Javascript Homework
Practice Problems:
Problem 1: Age and Movie Rating
Write a program that asks for a person’s age. If they are under 13, print “You can watch G or PG movies.”
- If they are 13–17, print “You can watch PG-13 movies.”
- If they are 18 or older, print “You can watch R-rated movies.”
%%js
let age = parseInt(prompt("Enter age:"));
// Nested conditionals
if (age < 13) {
console.log("You can watch G or PG movies.");
} else {
if (age < 18) {
console.log("You can watch PG-13 movies.");
} else {
console.log("You can watch R-rated movies.");
}
}
<IPython.core.display.Javascript object>
Problem 2: Grade Categorizer
Ask the user for a grade percentage (0–100).
- If the grade is 90 or higher, log “A”.
- Else if it is 80–89, log “B”.
- Else if it is 70–79, log “C”.
- Else if it is 60–69, log “D”.
- Otherwise, log “F”
%%js
let grade = parseInt(prompt("Enter grade percentage: "));
// Nested conditionals
if (grade >= 90) {
console.log("A");
} else {
if (grade >= 80) {
console.log("B");
} else {
if (grade >= 70) {
console.log("C");
} else {
if (grade >= 60) {
console.log("D");
} else {
console.log("F");
}
}
}
}
<IPython.core.display.Javascript object>