JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – Reverse array element but in the same position

JavaScript Quiz – Reverse array element but in the same position

Reverse array element –

In this, we are not going to reverse an array like element at index 0 places at the last index and vice versa.

But, what we do in reverse an array element. The array element is remaining at the same position or index but their value gets reversed. For examaple.

Original Array:-

[“Ajay”, “Yadav”]

Array Element Reversed :-

[“yajA”, “vadaY”]

I hope you understand the difference between the reverse array & reverse an array element.

JavaScript Question

In this javascript quiz code, you need to find out what happen when map, split, reverse and join together modify the array. And array you get is the new array. But what’s element value now.

First, we declare an variable array using const keyword and assign an javascript array to it.

let array = [“JS”, “Startup”];

Now, create an another variable reverse using const and assign an modified array of array variable.

const reverse = array.map(el => { … });

Finally, we are printing the output using a console log. Any guess what is the output of the below statement.

console.log(reverse[0]); // output => ?

console.log(reverse[1]); // output => ?

Answer – “SJ“, “putratS

Click To Run Code

Explanation

Now, you know the answer is “SJ, putratS” but why this. Let me explain

Array map method –

So, First we apply array map method to an array variable which not only iterate an array but also help to modify it and return a new array. Then inside of map, we get each element one by one.

array.map(el => { … })

String Split Method –

Then, we use a split method to break the string. After the split, its first element looks like.

el = “JS”


el.split() = [“J”, “S”]

Array Reverse Method –

In the next step, we use array reverse method which actually reverse an array.

el.split().reverse() = [“S”, “J”]

Array Join Method –

Now, we need to convert it back to a string so that it can be returned to the same position on the map.

el.split().reverse().join() = “SJ”

Finally, your reverse variable look like –

[“SJ”, “putratS”]

At last, That’s why we get an answer “SJ, putratS“.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JS Quiz – Is the String() and toString() are really same
  2. JavaScript Quiz – can in operator check value or index in an array
  3. JS Quiz – can really instanceof operator work on string literal
  4. JavaScript Quiz – Function can be used as class using new operator

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
0 Shares
Share via
Copy link