FAQ: Code Challenges: Intermediate JavaScript - reverseArray()

I like your solution! However I’m kind of confused as to how it works. Doesn’t the forEach method start with index 0 then moves through all the rest? My logic is telling me it should just return the same array that you put in, how is this working in reverse?

Hello, I wanted to share my solution. Any thoughts?

const reverseArray = arr => { const newArray = []; arr.forEach(item => newArray.unshift(item)); return newArray; } const sentence = ['sense.','make', 'all', 'will', 'This']; console.log(reverseArray(sentence))

my solution:

function reverseArray(array) {
    let newArray = [];
     for (const i in array) {
       newArray.unshift(array[i]);
         }
     return newArray
  }

I also remembered the method that puts everything at the beginning of the array :slight_smile:

const reverseArray = (array) => {
  const alteredArr = [];
  array.forEach(element =>{
    alteredArr.unshift(element);
  }
  )
  return alteredArr;
};

This article greatly helped me - thank you.

I was staring at the spare array with the square brackets, wondering why it was there, and I also didn’t understand why we were using .push, although I think I understand, now…

Hey, can anyone let me know why this solution does not count as a viable answer?

const reverseArray = array => { let newArray = []; for (let i = -1; i >= (0 - sentence.length); i--) { newArray.push(array.slice(i)[0]) } return newArray } const sentence = ['sense.','make', 'all', 'will', 'This']; console.log(reverseArray(sentence)) //Returns ['This', 'will', 'all', 'make', 'sense.'] console.log(sentence) //Returns ['sense.','make', 'all', 'will', 'This']

To my understanding, a new array was created and returns the ‘sentence’ array in reverse as requested

Just tossing this out there: Are there any edge cases?

Noticed on line 3 sentence instead of array.

Once that is fixed reverseArray([]) returns [], which is one edge case. Might be moot, though. Make the fix and test again.

It’s because you’re using “return” keyword in your for loop, so it stops after 1 cycle

1 Like

Hello fellow learners,

I would love to hear what you think about my following solution - changing the index-order through a little formula and the splice-method (Mdn):

const reverseArray = function(arra) { let reversed = []; for (let i = arra.length -1; i >= 0; i = i - 1) { reversed.splice((((i + 1)*-1)+arra.length), 0, arra[i]); } return reversed }; const sentence = ['sense.','make', 'all', 'will', 'This']; console.log(reverseArray(sentence))

Is there maybe a more elegant way to manipulate the index directly?

Do you mean one that does not use the .splice() method? Yes, there is:

    reversed.push(arra[i]);

Why can’t I combine “reversed.push(arr[i]);” with “return reversed.push(arr[i]);” ? When I combine that line of code I get undefined. However, isnt reversed already defined by an empty array?

const reverseArray = arr => { let reversed = []; for (let i = arr.length - 1; i >= 0; i--) { reversed.push(arr[i]); } return reversed } const sentence = ['sense.','make', 'all', 'will', 'This']; console.log(reverseArray(sentence))
arr = []
console.log(arr.push(42))

What is going to be logged, above?

arr = [] console.log(arr.push(42))

Output is 1.
I would have assumed it would have pushed the number 42 into the array.

Sorry, I am not making the connection other than both output to 1.

If we read the docs on the Array.prototype.push() method we find that the method has a return value, namely the new length of the array after the object has been pushed on to it. This value is meaningless if we are not specifically looking for it.

Bottom line, if you are in a loop and pushing values on to an array, don’t have return inside the loop, just do the push and complete the iteration. Then return the completed array.

1 Like

Let me share my solution with you guys!

const reverseArray = arr => {
  let newArray = [];
  for (let i = arr.length-1; i >= 0; i--) {
    newArray.push(arr[i]);
  }
  return newArray;
};

Just wanted to share my solution. Never really understood recursion but finally got it to work so felt like sharing it!

const reverseArray = array => {
  if (array.length === 1 || array.length === 0) {
    return array;
  } 

  const firstItem = array.shift();
  reverseArray(array);
  array.push(firstItem);
  return array;
}

Thank you, this fixed my issue with the function declaration solution!