Good afternoon, I'm new to Kotlin and working thro...
# getting-started
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.
r
Please don't cross post
d
Ok thanks for informing me, do you think you could answer the question?
c
Could you put some code that demonstrates the issue on https://try.kotlinlang.org/ ?
Oh wait - it looks like the MINOR_HEALTH range covers the IS_BLESSED range
t
Also I thought 100..89 doesn’t work? Doesn’t it have to be 89..100 or 100 downTo 89 (or w/e the operator was called?)
r
@Dangelomjoyce I believe your question has been answered multiple times both here and in #general. If you still have questions, could you be more specific as to what you still need help with? To summarize, you seem to have several issues: 1.
100..89
is not a valid range 2.
75..98
covers the range
75..89
, so the second will never be executed 3.
if (isBlessed) { IS_BLESSED } else {}
does not return a value in both branches If you could post an explanation of what you are trying to achieve (what ranges and flags result in which values), we could probably help you write the appropriate clauses.
2