Good afternoon, I'm new to Kotlin and working thro...
# announcements
d
Good afternoon, I'm new to Kotlin and working through a Big Nerd Ranch book, I came across the function in their code and it isn't working, can someone help me with the solution.val healthStatus = when (healthpoints) { in 100..89 -> EXCELLENT_HEALTH in 90..99 -> GOOD_HEALTH in 75..98 -> MINOR_HEALTH in 75..89 -> if (isBlessed) {IS_BLESSED } else { } in 15..74 -> LOW_HEALTH else -> POOR_HEALTH } ... println("$name $healthStatus")isBlessed is set to true, and I have a string stored in const val IS_BLESSED. The issue I am having is that string is not printing regardless the range of the healthpoints.
d
Does not look like the code should even compile - there's no completion of the
else
branch that would be required by the outer assignment.
Please put code in triple back-ticks, in these channels;
Copy code
println("So that it appears like this")
d
What is a triple backtick?\
I may just go back to using if else statements until I have a better understanding of when...worked fine before then. Thanks.
i
when
clauses are evaluated one-by-one. So the first range clause 75..98 catches all values in that range and thus you'll never step in the next branch.
Also
100..89
range looks strange: it is always empty, because its end is less than the start.