We're a place where coders share, stay up-to-date and grow their careers.
1) How to reverse a string using reduce method?
const rev = str => [...str].reduce((carry, char) => char.concat(carry), '')
Specifically, [...str] is superior to str.split('') because it correctly handles Unicode:
[...str]
str.split('')
'😎'.split('') // ["�", "�"] [...'😎'] // ["😎"]
2) What is the difference between slice and splice?
slice copies a range out of an array. splice deletes and/or inserts items at a given position in an array.
slice
splice
3) How to convert an object into a string?
The question's a little to broad imo, I'm gonna spare the time writing out exactly what Avalander did.
Specifically,
[...str]
is superior tostr.split('')
because it correctly handles Unicode:slice
copies a range out of an array.splice
deletes and/or inserts items at a given position in an array.The question's a little to broad imo, I'm gonna spare the time writing out exactly what Avalander did.