Code-Wars Kata Coding Challenge — “Stop gninnipS My sdroW!”
This is a basic code challenge which helps you to flex your Data Structure & Algorithms knowledge, problem solving skills and more importantly improve your computational thinking (:
Requirements —
Write a function called spinWords that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed . Strings passed in will consist of only letters and spaces. Spaces will be included only when more than one word is present.
Example —
spinWords( “Hey fellow warriors” ) => returns “Hey wollef sroirraw”spinWords( “This is a test”) => returns “This is a test”
Explanation -
// loop through the input argument/given string, ideally done by converting the string of words into an array of words using the JavaScript built in Array.split() method
// then mapping through this array with Array.map() method which returns an array of results
//check for any words that have more than four characters by using the String.length property using a ternary operator
// if so split such a word, reverse it using the String.reverse() method and join it again using the String.join() method
// if no such words are found, simply return them as part of the output array as the Array.map() method always returns an array of expected results
//finally convert this array of results into a string with spacing using the join() method
Please note — the spaces between the join() and split() methods have to be taken care of as then can be tricky to enable spacing amongst the chosen characters
Solution (ES6 one liner)-
return arg.split(‘ ’).map( item => item.length > 4 ? item.split(‘’).reverse().join(‘’): item).join(‘ ’);
Thanks for reading, stay tuned for more coding challenges and tech related articles !!!