If you want to answer part1 check out part1
1.How to reverse a String Using Reduce Method.
example: if you pass 'king' as an argument output is 'gnik'
2.what is the difference between slice and splice.
3.How to convert an object into a string?
If you want to answer part1 check out part1
1.How to reverse a String Using Reduce Method.
example: if you pass 'king' as an argument output is 'gnik'
2.what is the difference between slice and splice.
3.How to convert an object into a string?
For further actions, you may consider blocking this person and/or reporting abuse
Velan<> -
arjun -
Gabor Szabo -
sharathchandark -
Top comments (19)
Kinda depends on what you want.
Interesting fact for completeness: The JSON counterpart of
toString()
istoJSON()
. If an object has atoJSON()
method, it will be evaluated and its result will be stringified instead:Nice! I wasn't aware of that!
Then we could potentially override that method to get
[object Object]
when converting it to JSON.That sounds... useful? π
But yeah, it's a feature that I as well only discovered after many years of JS experience and since have never used in production. It's pretty cool though.
Oh, and also the result would in fact be
"[object Object]"
. The quotes are part of the serialized result (since it needs to be valid JSON).I mean, the specification is that the object gets converted to a string. About the actual content of the string, the OP said nothing π
Slight error in your example: Twilight is an Alicorn. Source β€οΈ
Haha, good catch! I'd update my post but I'm too lazy, so let's say I'm referencing the first season.
#2
// .slice returns a shallow copy of "substring" of elements in an Array
// .splice removes elements from elements at an index and can insert as well
#3
This is a bit of a trick question and depends on what you're looking for, and @avalander went into a lot of detail for this
1.How to reverse a String Using Reduce Method.
2.What is the difference between slice and splice?
3.How to convert an object into a string?
"Hello World".split('').reduce((a, b) => b + a)
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.
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 useJSON.stringify(obj)
Wow i was expecting this answer for reversing a string you can even do
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.
"king".split("").reverse().join(""); // result will be "gnik"
You could also accomplish this with a simple loop
var str = "king";
var reversed = "";
var i = str.length-1;
while (i >=0) {
reversed += str[i];
i--;
}
// result in the reversed variable will be "gink"
This one is constantly confused across JS developers. Splice actually returns a mutated version of the original array, and in the process actually modifies the array. Slice on the other hand can return a copy or a mutated version of the array, but the original array remains the way it was before the operation was executed. For example, if you try arr.splice(1, 1); it will literally remove a single item from an array starting at index 1. If you were to do the same thing with a slice operation, arr.slice(1, 1) it will start at index 1 and return 1 item in a new array (new array in memory). Splice also is used to manipulate arrays without the need to strore a returned result in a new variable since the original reference in memory is modified, whereas slice is intended to be used as a executable operation on an existing array and a returned value will either get passed as a parameter or stored in memory with it's own pointer and variable name.
Avalander's answer is pretty concise, but if I were to give an answer based on how the question is worded it would have to be JSON.stringify(obj);
Loving these threads, Sai!
var input = "king";
var reverseString = input.split("").reduce((accumulator, currentValue, index, array)=> currentValue + accumulator);
ELI5 version of #2: Slice returns a new array while splice modifies the original reference