0
0
PowerShellscripting~10 mins

Return values in PowerShell - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A+
B-
C/
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting to use the return keyword.
2fill in blank
medium

Complete 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'
ASize
BLength()
CCount
DLength
Attempts:
3 left
💡 Hint
Common Mistakes
Using Length() as a method instead of a property.
Using Count which is for collections, not strings.
3fill in blank
hard

Fix 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'
A^^
B^
C**
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using ^ which is bitwise XOR in PowerShell.
Using multiplication * which is not exponentiation.
4fill in blank
hard

Fill 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'
A%
B==
C-eq
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using == which is not the equality operator in PowerShell.
Using != which means not equal.
5fill in blank
hard

Fill 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'
ALength
B-gt
C-le
D-lt
Attempts:
3 left
💡 Hint
Common Mistakes
Using -le or -lt which mean less or less equal.
Using wrong property names or operators.