Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return a function that adds 5 to its input.
Kotlin
fun addFive(): (Int) -> Int {
return { x -> x + 5 }
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a lambda that subtracts or multiplies instead of adding.
Not returning a lambda at all.
✗ Incorrect
The function must return a lambda that adds 5 to the input number.
2fill in blank
mediumComplete the code to return a function that multiplies its input by 3.
Kotlin
fun multiplyByThree(): (Int) -> Int {
return { x -> x * 3 }
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of multiplication.
Dividing instead of multiplying.
✗ Incorrect
The returned lambda multiplies the input by 3.
3fill in blank
hardFix the error in the code to correctly return a function that subtracts 2 from its input.
Kotlin
fun subtractTwo(): (Int) -> Int {
return { x -> x - 2 }
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 2 instead of subtracting.
Subtracting input from 2 instead of the other way around.
✗ Incorrect
The lambda must subtract 2 from the input number.
4fill in blank
hardFill both blanks to create a function that returns a function which adds a given number to its input.
Kotlin
fun adder(n: Int): (Int) -> Int {
return { x -> x [1] n[2] }
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Adding extra operators after n.
✗ Incorrect
The function returns a lambda that adds n to x using the + operator. The second blank is a space to complete the expression.
5fill in blank
hardFill all three blanks to create a function that returns a function which multiplies its input by a given number and then adds another number.
Kotlin
fun multiplyAndAdd(multiplier: Int, addend: Int): (Int) -> Int {
return { x -> x [1] multiplier [2] addend[3] }
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or division instead of addition.
Mixing the order of operations.
✗ Incorrect
The lambda multiplies x by multiplier using * and then adds addend using +. The last blank is a space to complete the expression.