Skip to content

Instantly share code, notes, and snippets.

@giannispan
Created February 1, 2016 17:16
Show Gist options
  • Save giannispan/d887ce9a161f59d2f084 to your computer and use it in GitHub Desktop.
Save giannispan/d887ce9a161f59d2f084 to your computer and use it in GitHub Desktop.
Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed (Just like the name of this Kata). Strings passed in will consist of only letters and spaces. Spaces will be included only when more than one word is present.
function spinWords(str){
var splitWords = str.split(' ');
var length = splitWords.length;
for(var i = 0; i < length; i++)
{
if (splitWords[i].length >= 5 )
{
splitWords[i] = reverseWords(splitWords[i]);
}
}
return splitWords.join(' ');
}
function reverseWords(str) {
var i=str.length;
i=i-1;
var reversedwords="";
for (var x = i; x >=0; x--)
{
reversedwords +=(str.charAt(x));
}
return reversedwords;
}
@Setex10
Copy link

Setex10 commented Sep 23, 2022

i did it so

const reverseString = (string) => {
  const re = / /g;
  const stringArray = string.split(re)
  
  return (
  [stringArray.map(element => {
    return (element.length >= 5 ? 
            [...element].reverse().join(" ").replace(re, "") 
            : element)
  })]
  )[0].join(" ")
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment