Challenge - 5 Problems
PowerShell If-Else Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of nested if-elseif-else in PowerShell
What is the output of this PowerShell script?
PowerShell
param($num = 15) if ($num -lt 10) { Write-Output "Less than 10" } elseif ($num -lt 20) { Write-Output "Between 10 and 19" } else { Write-Output "20 or more" }
Attempts:
2 left
💡 Hint
Check which condition matches the value 15.
✗ Incorrect
The variable $num is 15. The first condition ($num -lt 10) is false. The elseif condition ($num -lt 20) is true, so it outputs 'Between 10 and 19'.
💻 Command Output
intermediate2:00remaining
PowerShell if-elseif-else with string comparison
What will this PowerShell script output when $color is set to 'blue'?
PowerShell
$color = 'blue' if ($color -eq 'red') { Write-Output 'Stop' } elseif ($color -eq 'yellow') { Write-Output 'Caution' } elseif ($color -eq 'green') { Write-Output 'Go' } else { Write-Output 'Unknown color' }
Attempts:
2 left
💡 Hint
Check if 'blue' matches any of the specified colors.
✗ Incorrect
The variable $color is 'blue', which does not match 'red', 'yellow', or 'green', so the else block runs and outputs 'Unknown color'.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this PowerShell if-elseif-else block
Which option contains the syntax error in this PowerShell code snippet?
PowerShell
if ($x -gt 10) { Write-Output 'Greater than 10' } elseif ($x -lt 5) { Write-Output 'Less than 5' } else Write-Output 'Between 5 and 10'
Attempts:
2 left
💡 Hint
Check the else block syntax carefully.
✗ Incorrect
The else block lacks an opening brace '{' before the Write-Output statement, causing a syntax error.
🔧 Debug
advanced2:00remaining
Why does this PowerShell script not output anything?
Given this script, why is there no output?
PowerShell
$value = 0 if ($value) { Write-Output 'Value is true' } elseif (-not $value) { Write-Output 'Value is false' } else { Write-Output 'Value is unknown' }
Attempts:
2 left
💡 Hint
Remember how PowerShell treats 0 in boolean context.
✗ Incorrect
In PowerShell, 0 is treated as false. So the if ($value) condition is false, the elseif (-not $value) is true, so it outputs 'Value is false'.
🚀 Application
expert3:00remaining
Determine the final value of $result after this PowerShell script runs
What is the value of $result after running this script?
PowerShell
$score = 85 if ($score -ge 90) { $result = 'A' } elseif ($score -ge 80) { $result = 'B' } elseif ($score -ge 70) { $result = 'C' } elseif ($score -ge 60) { $result = 'D' } else { $result = 'F' }
Attempts:
2 left
💡 Hint
Check the conditions in order and see which one matches 85 first.
✗ Incorrect
85 is not greater or equal to 90, so first if fails. 85 is greater or equal to 80, so $result is set to 'B'. The rest are skipped.