DEV Community

loading...

Discussion on: JavaScript Quiz Part 2

Collapse
terabaud profile image
Lea Rosema (she/her) • Edited
  1. "Hello World".split('').reduce((a, b) => b + a)

  2. slice is a prototype function on Array and String. It doesn't mutate and returns a new Array/String that equals to an extract of the Array/String. splice is a prototype function on Array and is used to insert and delete elements at a given index of the array. It mutates the actual array and returns the deleted elements.

  3. You can explicitly call obj.toString() or you can use JavaScript's coercion: (obj + ""), so the obj.toString() method is implicitly called to convert it to a string. By default, the output is not too useful; it returns [object Object], but you can override the toString() method. Or, if you want to serialize the object into a string representation, you may want to use JSON.stringify(obj)

Collapse
sait profile image
Sai gowtham Author

Wow i was expecting this answer for reversing a string you can even do

const stringRev = (str)=>[...str].reduce((acc,e)=>e+acc)