Last active
September 30, 2021 13:32
-
-
Save Miniwe/1d7ee5ae4b1ca5ecf4099de54f36767b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const str = 'i love codewars' | |
// s rawe docevoli | |
function reverse (str) { | |
let res = '' | |
const spaces = []; | |
Array.from(str).forEach((item, index) => { | |
if (item === ' ') { | |
spaces.push(index) | |
} | |
}) | |
return Array.from(str).reverse().reduce((res,item) => { | |
let newStr = res; | |
if (item !== ' ') { | |
newStr += item | |
} | |
if (spaces.indexOf(newStr.length) > -1) { | |
newStr += ' ' | |
} | |
return newStr | |
}, '') | |
} | |
console.log(reverse(str)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment