-
Notifications
You must be signed in to change notification settings - Fork 177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: how to solve for_loops_two with only one line of change? #56
Comments
Yes, this koan is broken, but this project is dead. |
Not sure if you consider this one line but it's one statement: sentence = if sentence.is_empty() {
word.to_string()
} else {
sentence + " " + word
}; |
If you don't mind refactoring the koan, you could reduce it to the following: let sentence: String = words.join(" "); I'm unaware of a solution that would be a one-line expression inside of the loop |
Here's what I did: sentence = (sentence + " " + word).trim().to_string(); |
my normal tack in a loop (when there is no if !sentence.is_empty() { sentence += " " }
sentence += *word; (no idea how many more semicolons are due here -- also learning for the first time today! and glad to have this resource even if it has a few rough edges) |
Usually the koans can be solved by only changing the marked line. I cannot figure out how to conditionally append a space except after the last element of the array. In the for loop, how do we know we've reached the end, without keeping a counter, or refactoring the code? Thanks!
The text was updated successfully, but these errors were encountered: