1

var reverse = function (list) {
  return list.reduce(function (reversedList, current) {
    return [ current ].concat(reversedList);
  }, []);
};

console.log(reverse([1,2,3,4]));

So this is for reversing an array in Javascript using reduce. According to MDNs ref. The second argument(here current) is the second element after the first element of the array if no initialValue is provided. But in this case, the current is not the second element but the last element of the array. Why is it so?

You can run this code on the console for some array [1,2,3,4] and current will be returning 4.

5
  • 1
    Your function works fine for me. reduce will process 'current' in the order the array has them in, according to JS spec. Current will be 4 after the function runs because that is the last item in the array. Commented Dec 1, 2015 at 17:27
  • The way reduce works, you will go through that callback function multiple times (4 if you pass [1,2,3,4]). The first time through, the first arg will be your empty array and the second argument will be the first item in the array. The second time through, the first arg will be what the callback returned the first time and the second will be the second item in the array. That continues till the end of the array. By "current" what is meant is the item at the index the loop is currently at. Commented Dec 1, 2015 at 17:28
  • Are you seeing something in your console like this: Array[4] That means it is an array with 4 items in it. Commented Dec 1, 2015 at 17:31
  • @mcgraphix well that's okay, I get that part. But how is the reverse happening here. current is not the index number as far as I get it. What's the point of keeping it inside squared brackets. Commented Dec 1, 2015 at 17:37
  • Too much code for a comment. See below. Commented Dec 1, 2015 at 17:48

1 Answer 1

3

What your code is doing is creating a new array with the current item in it when you do this: [current] and then concatenating the "reversedList" array after it.

var reverse = function (list) {
  return list.reduce(function (reversedList, current) {
    //this creates a new array with just the "current" item in it
    //and then concatenates the "reversedList" array with it.

    console.log("concatenating ", current, "with", reversedList); 
    return [ current ].concat(reversedList);

  }, []);
};

console.log(reverse([1,2,3,4]));

This is kind of a weird way to go about doing that but I assume it is just for educational purposed. You could just add each item to the beginning of the array rather than concatenating.

 var reverse = function (list) {
  return list.reduce(function (reversedList, current) {
    reversedList.unshift(current);
    return reversedList;
  }, []);
};

console.log(reverse([1,2,3,4]));

That said, if you are just trying to reverse an array you can use the reverse() method:

console.log([1,2,3,4].reverse());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.