📖 Scenario: You are helping a small shop decide if a customer gets a discount based on their purchase amount.
🎯 Goal: Build a simple program that checks if the purchase amount is enough for a discount and shows the correct message.
📋 What You'll Learn
Create a variable to store the purchase amount.
Create a variable to store the discount threshold.
Use an if-else statement to check if the purchase amount is greater than or equal to the discount threshold.
Print a message showing if the customer gets a discount or not.
💡 Why This Matters
🌍 Real World
Stores often give discounts based on how much customers buy. This helps encourage bigger purchases.
💼 Career
Understanding if-else statements is important for making decisions in programs, like showing messages or calculating prices.
Progress0 / 4 steps
1
Create the purchase amount variable
Create a variable called purchaseAmount and set it to 120.
Javascript
Hint
Use let to create the variable and assign the number 120.
2
Create the discount threshold variable
Create a variable called discountThreshold and set it to 100.
Javascript
Hint
Use let to create the variable and assign the number 100.
3
Write the if-else statement
Write an if statement that checks if purchaseAmount is greater than or equal to discountThreshold. Inside the if, create a variable called message and set it to "You get a discount!". In the else part, set message to "No discount this time.".
Javascript
Hint
Use if (purchaseAmount >= discountThreshold) to check the condition. Then assign the correct message inside each block.
4
Print the message
Write console.log(message) to print the message to the console.
Javascript
Hint
Use console.log(message) to show the message in the console.
Practice
(1/5)
1. What does an if-else statement do in JavaScript?
easy
A. It repeats a block of code multiple times.
B. It creates a new variable.
C. It defines a function to be used later.
D. It chooses between two actions based on a condition.
Solution
Step 1: Understand the purpose of if-else
An if-else statement lets the program decide between two paths depending on a condition.
Step 2: Match the description to the options
Only It chooses between two actions based on a condition. describes choosing between two actions based on a condition.
Final Answer:
It chooses between two actions based on a condition. -> Option D
Quick Check:
If-else = choose between two actions [OK]
Hint: If-else picks one of two paths based on true or false [OK]
Common Mistakes:
Confusing if-else with loops
Thinking if-else defines functions
Believing if-else creates variables
2. Which of the following is the correct syntax for an if-else statement in JavaScript?
easy
A. if (condition) { } else { }
B. if condition { } else { }
C. if (condition) then { } else { }
D. if condition then { } else { }
Solution
Step 1: Recall JavaScript if-else syntax
JavaScript requires parentheses around the condition and braces for blocks: if (condition) { } else { }
Step 2: Compare options to correct syntax
Only if (condition) { } else { } uses parentheses and braces correctly without extra keywords.
Final Answer:
if (condition) { } else { } -> Option A
Quick Check:
Correct syntax uses parentheses and braces [OK]
Hint: Remember: condition in ( ), blocks in { } [OK]
Common Mistakes:
Omitting parentheses around condition
Adding 'then' keyword (not in JS)
Missing braces for code blocks
3. What will be the output of this code?
let age = 18;
if (age >= 18) {
console.log('Adult');
} else {
console.log('Minor');
}
medium
A. Adult
B. Minor
C. undefined
D. Error
Solution
Step 1: Evaluate the condition age >= 18
Since age is 18, the condition age >= 18 is true.
Step 2: Determine which block runs
The if block runs and prints 'Adult'. The else block is skipped.
Final Answer:
Adult -> Option A
Quick Check:
Condition true -> if block runs -> 'Adult' [OK]
Hint: Check if condition is true or false to pick output [OK]
Common Mistakes:
Thinking 18 is less than 18
Confusing else block runs when condition is true
Expecting syntax error
4. Find the error in this code:
let score = 75;
if score >= 60 {
console.log('Pass');
} else {
console.log('Fail');
}
medium
A. Using else without if
B. Missing semicolon after console.log
C. Missing parentheses around the condition
D. Variable score is not declared
Solution
Step 1: Check if-else syntax
JavaScript requires parentheses around the condition in if statements.
Step 2: Identify the missing parentheses
The code has if score >= 60 { ... } but it should be if (score >= 60) { ... }
Final Answer:
Missing parentheses around the condition -> Option C
Quick Check:
if condition must be in ( ) [OK]
Hint: Always put condition inside ( ) after if [OK]
Common Mistakes:
Forgetting parentheses around condition
Thinking semicolon is required after console.log block
Confusing else usage
5. You want to print 'Even' if a number is even, and 'Odd' if it is odd. Which code correctly uses if-else to do this?
let num = 7;
// Choose the correct code snippet
hard
A. if (num % 2 != 0) {
console.log('Even');
} else {
console.log('Odd');
}
B. if (num % 2 === 0) {
console.log('Even');
} else {
console.log('Odd');
}
C. if (num % 2) {
console.log('Even');
} else {
console.log('Odd');
}
D. if (num / 2 === 0) {
console.log('Even');
} else {
console.log('Odd');
}
Solution
Step 1: Understand how to check even numbers
A number is even if remainder when divided by 2 is zero: num % 2 === 0.
Step 2: Check each option's condition
if (num % 2 === 0) {
console.log('Even');
} else {
console.log('Odd');
} correctly checks num % 2 === 0. Others either use wrong operator or logic.
Final Answer:
if (num % 2 === 0) { console.log('Even'); } else { console.log('Odd'); } -> Option B
Quick Check:
Even check uses num % 2 === 0 [OK]
Hint: Use modulo (%) to test evenness: zero remainder means even [OK]
Common Mistakes:
Using division (/) instead of modulo (%)
Checking if remainder is truthy instead of zero
Reversing even and odd conditions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.