Skip to content

Latest commit

 

History

History
33 lines (23 loc) · 1.01 KB

stop-gninnips-my-sdrow.md

File metadata and controls

33 lines (23 loc) · 1.01 KB

Stop gninnipS My sdroW! 6 Kyu

LINK TO THE KATA - STRINGS ALGORITHMS

Description

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.

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"

Solution

const reverseWord = string => string.split('').reverse().join('')

const spinWords = string => {
  const words = string.split(' ')

  const result = words.map(word => (word.length > 4 ? reverseWord(word) : word))

  return result.join(' ')
}