You are reversing the characters of the whole string, then reversing the words. The if statement has no effect because you are checking the length of original string every time (not the words).
Try this:
function spinWords(str) { let words = str.split(" "); let result = []; for(let word of words) { let newWord = word.length >= 5 ? word.split("").reverse().join("") : word; result.push(newWord); } return result.join(" "); } spinWords("Hey fellow warriors");
or this:
function spinWords(str) { let words = str.split(" "); let result = []; for(let word of words) { if(word.length >= 5) { result.push(word.split("").reverse().join("")); } else { result.push(word); } } return result.join(" "); } spinWords("Hey fellow warriors");
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You are reversing the characters of the whole string, then reversing the words. The if statement has no effect because you are checking the length of original string every time (not the words).
Try this:
or this: