📖 Scenario: Imagine you are building a simple program that helps a coffee shop decide what message to show customers based on the time of day.
🎯 Goal: You will create a small program that uses conditional logic to show different messages depending on the time.
📋 What You'll Learn
Create a variable to hold the current hour of the day
Create a variable to hold a message
Use conditional logic with if and else to set the message based on the hour
Print the message to the console
💡 Why This Matters
🌍 Real World
Conditional logic helps programs make choices, like showing different messages or taking different actions based on information.
💼 Career
Understanding conditional logic is essential for all programming jobs because it lets software respond to different situations.
Progress0 / 4 steps
1
DATA SETUP: Create a variable for the current hour
Create a variable called currentHour and set it to the number 9.
Javascript
Hint
Use const to create the variable and assign the number 9.
2
CONFIGURATION: Create a variable for the message
Create a variable called message and set it to an empty string "".
Javascript
Hint
Use let because the message will change later.
3
CORE LOGIC: Use conditional logic to set the message
Use if and else statements to set message to "Good morning!" if currentHour is less than 12, otherwise set it to "Good afternoon!".
Javascript
Hint
Use if (currentHour < 12) to check the time and set the message accordingly.
4
OUTPUT: Print the message
Write console.log(message) to display the message in the console.
Javascript
Hint
Use console.log(message) to show the message.
Practice
(1/5)
1. Why do we use conditional logic like if and else in JavaScript?
easy
A. To make decisions and run different code based on conditions
B. To repeat the same code many times
C. To store multiple values in a list
D. To create new variables
Solution
Step 1: Understand the purpose of conditional logic
Conditional logic allows the program to choose between different actions depending on conditions.
Step 2: Compare options with this purpose
Only To make decisions and run different code based on conditions describes making decisions based on conditions, which matches the purpose of if and else.
Final Answer:
To make decisions and run different code based on conditions -> Option A
Quick Check:
Conditional logic = decision making [OK]
Hint: Think: Does it help choose between actions? [OK]
Common Mistakes:
Confusing conditional logic with loops
Thinking it stores data
Believing it only creates variables
2. Which of the following is the correct syntax to check if a variable age is greater than 18 in JavaScript?
easy
A. if (age > 18) { }
B. if (age > 18) then { }
C. if age > 18 then { }
D. if age > 18 { }
Solution
Step 1: Recall JavaScript if statement syntax
JavaScript requires parentheses around the condition and curly braces for the code block: if (condition) { }.
Step 2: Check each option
if (age > 18) { } matches the correct syntax. Options A and C miss parentheses or use invalid keywords like 'then'. if (age > 18) then { } incorrectly uses 'then'.
Final Answer:
if (age > 18) { } -> Option A
Quick Check:
Correct if syntax = parentheses + braces [OK]
Hint: Remember: condition in ( ), code in { } [OK]
Common Mistakes:
Omitting parentheses around condition
Using 'then' keyword (not in JavaScript)
Missing curly braces
3. What will be the output of this code?
let score = 75;
if (score >= 60) {
console.log('Pass');
} else {
console.log('Fail');
}
medium
A. Fail
B. No output
C. Error
D. Pass
Solution
Step 1: Check the condition score >= 60
The variable score is 75, which is greater than or equal to 60, so the condition is true.
Step 2: Determine which block runs
Since the condition is true, the code inside the if block runs, printing 'Pass'.
Final Answer:
Pass -> Option D
Quick Check:
75 >= 60 is true, so output = Pass [OK]
Hint: Check if condition is true or false to pick output [OK]
Common Mistakes:
Confusing which block runs on true condition
Thinking output is 'Fail' for true condition
Assuming syntax error
4. Find the error in this code snippet:
let temperature = 30;
if temperature > 25 {
console.log('Hot');
} else {
console.log('Cold');
}
medium
A. Using else without if
B. Missing semicolon after variable declaration
C. Missing parentheses around the condition in if statement
D. Incorrect variable name
Solution
Step 1: Check the if statement syntax
JavaScript requires parentheses around the condition in an if statement, but here they are missing.
Step 2: Verify other parts
The variable declaration is correct with semicolon, else is correctly used, and variable name matches.
Final Answer:
Missing parentheses around the condition in if statement -> Option C
Quick Check:
if needs (condition) [OK]
Hint: Always put condition inside ( ) after if [OK]
Common Mistakes:
Forgetting parentheses in if condition
Thinking semicolon is mandatory after if
Confusing else usage
5. You want to write a program that prints 'Good morning' if the hour is less than 12, 'Good afternoon' if the hour is between 12 and 18 (inclusive), and 'Good evening' otherwise. Which code correctly uses conditional logic to do this?
hard
A. if (hour < 12) {
console.log('Good morning');
} else if (hour < 18) {
console.log('Good afternoon');
} else {
console.log('Good evening');
}
B. if (hour < 12) {
console.log('Good morning');
} else if (hour <= 18) {
console.log('Good afternoon');
} else {
console.log('Good evening');
}
C. if (hour <= 12) {
console.log('Good morning');
} else if (hour <= 18) {
console.log('Good afternoon');
} else {
console.log('Good evening');
}
D. if (hour < 12) {
console.log('Good morning');
} else if (hour > 12 && hour < 18) {
console.log('Good afternoon');
} else {
console.log('Good evening');
}
Solution
Step 1: Understand the conditions for each greeting
'Good morning' if hour < 12, 'Good afternoon' if hour between 12 and 18 inclusive, else 'Good evening'.
Step 2: Check each option's condition ranges
if (hour < 12) {
console.log('Good morning');
} else if (hour <= 18) {
console.log('Good afternoon');
} else {
console.log('Good evening');
} correctly uses hour < 12 for morning, hour <= 18 for afternoon (including 12 and 18), else evening. Others miss inclusive 12 or 18 or have wrong ranges.
Final Answer:
if (hour < 12) {
console.log('Good morning');
} else if (hour <= 18) {
console.log('Good afternoon');
} else {
console.log('Good evening');
} -> Option B
Quick Check:
Inclusive afternoon means <= 18, so if (hour < 12) {
console.log('Good morning');
} else if (hour <= 18) {
console.log('Good afternoon');
} else {
console.log('Good evening');
} fits [OK]
Hint: Check if conditions cover all cases without gaps [OK]
Common Mistakes:
Using < instead of <= for inclusive ranges
Overlapping or missing ranges
Wrong logical operators in conditions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.