Creating a function to check number ranges

Report a typo

You are working on a function in Kotlin to check if an input number falls within certain ranges. You want your function to return 'Number is in range 1 to 10' if number is in the range from 1 to 10, 'Number is in range 11 to 20' if number is in range from 11 to 20, and 'Number is outside the range 1 to 20' if number is outside this range. Fill the blanks in code to complete the function following the above conditions.

Fill in the gaps with the relevant elements
fun checkNumber(number: Int): String {
    return (number) {
         1..10 -> "Number is in range 1 to 10"
        in 11..20 -> "Number is in range 11 to 20"
         -> "Number is outside the range 1 to 20"
    }
}

println(checkNumber(5))
println(checkNumber(15))
println(checkNumber(25))
wheninelse

Create a free account to access the full topic