0

How do i make it so the function will take in the param (breed) and search the uppercase letter and add a space there.

for example if i pass "goldenRetriever" as the param, then the function will transform it into "golden retriever"

function test(breed){
 for(i=1; i<breed.length; i++){
 //wat do i do here
 }
}
asked Feb 19, 2016 at 2:31
2
  • This smells so much like a homework assignment to me. Try searching, I'll start you off with the first solution: stackoverflow.com/questions/1027224/… Commented Feb 19, 2016 at 2:34
  • I wouldn't use a loop when .replace() with a regular expression can do it. Commented Feb 19, 2016 at 2:36

1 Answer 1

5

You could split the string before each uppercase letter using a regular expression with a positive lookahead, /(?=[A-Z])/, then you could join the string back together with a space and convert it to lowercase:

"goldenRetrieverDog".split(/(?=[A-Z])/).join(' ').toLowerCase();
// "golden retriever dog"

Alternatively, you could also use the .replace() method to add a space before each capital letter and then convert the string to lowercase:

"goldenRetrieverDog".replace(/([A-Z])/g, " 1ドル").toLowerCase();
// "golden retriever dog"
answered Feb 19, 2016 at 2:34
Sign up to request clarification or add additional context in comments.

1 Comment

@Firefalcon1155 Because you're not returning the string. It should be return breed.split(/(?=[A-Z])/).join(' ').toLowerCase();.. see this example -> jsfiddle.net/L7m2x7uL

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.