The else
part is indeed optional, but the return value of a method is the value of the last expression in the method. So, when you don't put the else part in an actual else
block, you always return that value. As a solution, you either use an if/else
expression, which is the preferred way, or you use an actual return
statement in the if
statement.
// The return value of the method is the value of the if/else expression.
if (numer == denom) {
"1"
} else {
numer + "/" + denom
}
or
if (numer == denom) {
return "1"
}
numer + "/" + denom