Complete the code to repeat the print statement 3 times using the repeat function.
repeat([1]) { println("Hello") }
The repeat function takes the number of times to repeat as its argument. Here, 3 repeats the print statement 3 times.
Complete the code to print the numbers 0 to 4 using repeat and the loop index.
repeat(5) { [1] -> println([1]) }
The lambda parameter in repeat is commonly named i to represent the current index from 0 up to one less than the repeat count.
Fix the error in the code to correctly repeat printing "Hi" 4 times.
repeat([1]) { println("Hi") }
The repeat function expects an integer argument without quotes. Using 4 (an Int) works correctly.
Fill both blanks to create a map of numbers to their squares using repeat.
val squares = mutableMapOf<Int, Int>() repeat([1]) { [2] -> squares[[2]] = [2] * [2] }
We repeat 5 times and use i as the index variable to fill the map with numbers and their squares.
Fill all three blanks to create a list of even numbers using repeat and an if condition.
val evens = mutableListOf<Int>() repeat([1]) { [2] -> if ([2] % 2 == 0) { evens.add([2]) } }
We repeat 10 times, use i as the index, and add i to the list if it is even.