What happens here is that I am trying to reinforce my knowledge in JS
, practicing certain challenges in codewars . The challenge I am doing now is about the following:
"Write a function that takes a string of one or more words and returns the same string, but with the five or more letters reversed. Passed strings will consist of only letters and spaces. Spaces will be included only when more than one word is present ."
Examples:
spinWords( "Hey fellow warriors" ) => returns "Hey wollef sroirraw"
spinWords( "This is a test") => returns "This is a test"
spinWords( "This is another test" )=> returns "This is rehtona test"
What I did to fulfill that condition is to put each word in the same array and put each one of those in an array separated by letters. My problem is that codewars
when testing my function returns ""
and I think the problem is in the passage of the array to string but I don't know what it can be. If someone could explain to me why this is happening, I would really appreciate it.
What is expected in the output would be the same text string but with the words inverted whose letters are equal to 5 or more.
function spinWords(str){
let splittedWord = str.split(" "), arr = [], result;
for(let i = 0; i < splittedWord; i++){
arr.push(splittedWord[i].split(""));
if(arr[i].length >= 5){
arr[i].reverse();
}
}
result = arr.join(" ");
return result;
}
Your code has 4 errors:
arr[i].reverse()
returns an inverted array that needs to be saved somewhere, I changed it toarr[i] = arr[i].reverse();
result = arr.join(" ")
, arr contains multiple arrays so you have to iterate through it and do a join for each one.join("")
don't have to take space