Bird
Raised Fist0
Javascriptprogramming~20 mins

Why conditional logic is needed in Javascript - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Conditional Logic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this conditional code?
Look at this JavaScript code. What will it print to the console?
Javascript
const age = 20;
if (age >= 18) {
  console.log('Adult');
} else {
  console.log('Minor');
}
A"Adult"
B"Minor"
Cundefined
DSyntaxError
Attempts:
2 left
💡 Hint
Check the condition if age is 18 or more.
🧠 Conceptual
intermediate
1:30remaining
Why do we use conditional logic in programs?
Choose the best reason why conditional logic is important in programming.
ATo repeat the same code many times
BTo make decisions and run different code based on conditions
CTo store data permanently
DTo make the program run faster
Attempts:
2 left
💡 Hint
Think about how a program chooses what to do next.
❓ Predict Output
advanced
2:00remaining
What will this nested conditional code output?
Check the output of this JavaScript code with nested if-else statements.
Javascript
const score = 75;
if (score >= 90) {
  console.log('Grade A');
} else if (score >= 70) {
  console.log('Grade B');
} else {
  console.log('Grade C');
}
A"Grade A"
BReferenceError
C"Grade C"
D"Grade B"
Attempts:
2 left
💡 Hint
Look at the score and check which condition it meets first.
🔧 Debug
advanced
2:00remaining
Which option causes an error in this conditional code?
Find which code snippet will cause an error when run.
Javascript
const temperature = 30;
if temperature > 25 {
  console.log('Hot');
} else {
  console.log('Cold');
}
Aif temperature > 25 { console.log('Hot'); } else { console.log('Cold'); }
Bif (temperature > 25) { console.log('Hot'); } else { console.log('Cold'); }
Cif (temperature > 25) console.log('Hot'); else console.log('Cold');
Dtemperature > 25 ? console.log('Hot') : console.log('Cold');
Attempts:
2 left
💡 Hint
Check the syntax of the if statement carefully.
🚀 Application
expert
2:30remaining
What is the value of variable result after this code runs?
Consider this code using conditional logic and the ternary operator. What is the final value of result?
Javascript
const num = 10;
const result = num % 2 === 0 ? 'Even' : 'Odd';
A10
B"Odd"
C"Even"
Dundefined
Attempts:
2 left
💡 Hint
Check if 10 is divisible by 2 without remainder.

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

  1. Step 1: Understand the purpose of conditional logic

    Conditional logic allows the program to choose between different actions depending on conditions.
  2. 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.
  3. Final Answer:

    To make decisions and run different code based on conditions -> Option A
  4. 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

  1. Step 1: Recall JavaScript if statement syntax

    JavaScript requires parentheses around the condition and curly braces for the code block: if (condition) { }.
  2. 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'.
  3. Final Answer:

    if (age > 18) { } -> Option A
  4. 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

  1. 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.
  2. Step 2: Determine which block runs

    Since the condition is true, the code inside the if block runs, printing 'Pass'.
  3. Final Answer:

    Pass -> Option D
  4. 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

  1. Step 1: Check the if statement syntax

    JavaScript requires parentheses around the condition in an if statement, but here they are missing.
  2. Step 2: Verify other parts

    The variable declaration is correct with semicolon, else is correctly used, and variable name matches.
  3. Final Answer:

    Missing parentheses around the condition in if statement -> Option C
  4. 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

  1. 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'.
  2. 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.
  3. Final Answer:

    if (hour < 12) { console.log('Good morning'); } else if (hour <= 18) { console.log('Good afternoon'); } else { console.log('Good evening'); } -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard