Skip to content
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

Open
wh5a opened this issue Oct 18, 2020 · 5 comments
Open

Question: how to solve for_loops_two with only one line of change? #56

wh5a opened this issue Oct 18, 2020 · 5 comments

Comments

@wh5a
Copy link

wh5a commented Oct 18, 2020

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!

@janek37
Copy link

janek37 commented Jan 10, 2021

Yes, this koan is broken, but this project is dead.

@ghost
Copy link

ghost commented Mar 19, 2021

Not sure if you consider this one line but it's one statement:

sentence = if sentence.is_empty() {
    word.to_string()
} else {
    sentence + " " + word
};

@kenaniah
Copy link

kenaniah commented Apr 14, 2021

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

@benash
Copy link

benash commented Mar 25, 2022

Here's what I did:

sentence = (sentence + " " + word).trim().to_string();

@benmosher
Copy link

benmosher commented Oct 23, 2022

my normal tack in a loop (when there is no .join(" ") in the platform):

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants