Skip to content

Instantly share code, notes, and snippets.

@Miniwe
Last active September 30, 2021 13:32
Show Gist options
  • Save Miniwe/1d7ee5ae4b1ca5ecf4099de54f36767b to your computer and use it in GitHub Desktop.
Save Miniwe/1d7ee5ae4b1ca5ecf4099de54f36767b to your computer and use it in GitHub Desktop.
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