Challenge - 5 Problems
Split and Join Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of splitting a string with multiple spaces
What is the output of this Ruby code?
Ruby
text = "apple banana cherry" result = text.split(" ") puts result.inspect
Attempts:
2 left
💡 Hint
The split method with a space splits exactly on each space character.
✗ Incorrect
Using split(" ") splits the string at every single space, so multiple spaces create empty strings in the array.
❓ Predict Output
intermediate2:00remaining
Joining array elements with a custom separator
What is the output of this Ruby code?
Ruby
arr = ["red", "green", "blue"] result = arr.join("-") puts result
Attempts:
2 left
💡 Hint
The join method uses the string you give as a separator between elements.
✗ Incorrect
The join method combines array elements into one string, placing the given separator between them.
🔧 Debug
advanced2:00remaining
Why does this split produce an empty first element?
Consider this Ruby code. Why does the first element of the result array become an empty string?
Ruby
text = ",one,two,three" result = text.split(",") puts result.inspect
Attempts:
2 left
💡 Hint
Think about what happens before the first comma.
✗ Incorrect
When the string starts with the separator, split returns an empty string for the part before the first separator.
❓ Predict Output
advanced2:00remaining
Splitting with limit parameter
What is the output of this Ruby code?
Ruby
text = "a,b,c,d" result = text.split(",", 3) puts result.inspect
Attempts:
2 left
💡 Hint
The limit parameter controls how many parts the string is split into.
✗ Incorrect
With limit 3, split divides the string into at most 3 parts, so the last part contains the rest of the string.
🧠 Conceptual
expert3:00remaining
Effect of split and join on string with multiple separators
Given the Ruby code below, what is the final output?
Ruby
text = "one,,two,,,three" parts = text.split(",") joined = parts.join(",") puts joined
Attempts:
2 left
💡 Hint
Think about how split handles consecutive separators and what join does with empty strings.
✗ Incorrect
Split creates empty strings for consecutive commas, and join puts commas back between all elements, preserving the original pattern.