Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the sum of two numbers.
PowerShell
function Add-Numbers {
param($a, $b)
return $a [1] $b
}
Add-Numbers 3 4 Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting to use the return keyword.
✗ Incorrect
The return statement returns the sum of $a and $b using the + operator.
2fill in blank
mediumComplete the code to return the length of a string.
PowerShell
function Get-Length {
param($text)
return $text.[1]
}
Get-Length 'hello' Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
Length() as a method instead of a property.Using
Count which is for collections, not strings.✗ Incorrect
The Length property returns the number of characters in the string.
3fill in blank
hardFix the error in the function to correctly return the square of a number.
PowerShell
function Get-Square {
param($num)
return $num [1] 2
}
Get-Square 5 Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
^ which is bitwise XOR in PowerShell.Using multiplication
* which is not exponentiation.✗ Incorrect
PowerShell uses ** for exponentiation to return the square of a number.
4fill in blank
hardFill both blanks to return only even numbers from a list.
PowerShell
$numbers = 1..10 function Get-Evens { param($nums) return $nums | Where-Object { $_ [1] 2 [2] 0 } } Get-Evens $numbers
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
== which is not the equality operator in PowerShell.Using
!= which means not equal.✗ Incorrect
The modulo operator % finds the remainder. -eq checks if remainder equals zero, meaning the number is even.
5fill in blank
hardFill all three blanks to return a dictionary of words and their lengths for words longer than 3 characters.
PowerShell
$words = @('cat', 'house', 'dog', 'elephant') $result = @{} foreach ($word in $words) { if ($word.[1] [2] 3) { $result[$word] = $word.[3] } } $result
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
-le or -lt which mean less or less equal.Using wrong property names or operators.
✗ Incorrect
Length gets the word length. -gt means greater than, so words longer than 3 are included.