0

I've looked at SO to see what's been said about this and I've done my homework regarding this subject but, in the solution to this problem from Eloquent JavaScript, I've done the following:

function reversedArray(array) {
    let reversedArray = [];
    for (let i = 0; i <= array.length; i++) {
        reversedArray.push(array[array.length - i]);
    }
    return reversedArray;
}
let arr = [
    1,
    2,
    3,
    4
];
console.log(arr);
console.log(reversedArray(arr));

There, I obtained this:

> 1,2,3,4
> ,4,3,2,1

Which means that the array.length of the reversed array is now longer than the original array by an extra element. Of course, I fixed my code to eliminate the first element of the reversed array like this:

function reversedArray(array) {
    let reversedArray = [];
    for (let i = 0; i <= array.length; i++) {
        reversedArray.push(array[array.length - i]);
    }
    reversedArray.shift();
    return reversedArray;
}
let arr = [
    1,
    2,
    3,
    4
];
console.log(arr);
console.log(reversedArray(arr));

And it worked as expected producing:

> 1,2,3,4
> 4,3,2,1

I am starting to learn JS and I can't see why the reversed array has an extra element. Can somebody explain it to me? I don't feel it is right to just eliminate the element that's extra and move on with life... I have looked at some other solutions and they are fine but I just want to learn please, why is it that my function increases the array length? Many thanks!

Diego
  • 328
  • 2
  • 9
  • 2
    `<` not `<=` in the loop. – Pointy Jan 14 '22 at 14:40
  • Let me try that @Pointy. Thanks! – Diego Jan 14 '22 at 14:41
  • 1
    `let i = 0`, `array[array.length - i]`… What index is `array.length - 0`…? – deceze Jan 14 '22 at 14:42
  • 3
    Why reinvent the wheel? What's wrong with [`array.reverse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse)? – BenM Jan 14 '22 at 14:42
  • I have tried @Pointy and it eliminated one of the elements of the array: ```> ,4,3,2``` – Diego Jan 14 '22 at 14:43
  • @BenM homework/code challenge – VLAZ Jan 14 '22 at 14:43
  • @BenM I am familiar with that function but the problem in the book specifically asks to do it from scratch and I am learning how to solve the problem. – Diego Jan 14 '22 at 14:44
  • @BenM it is not a homework. I am learning JS on my own and I have done most of the challenges from Grasshopper and now I am reading Eloquent JS. – Diego Jan 14 '22 at 14:45
  • @Déjàvu I am using Grasshopper's coding interface – Diego Jan 14 '22 at 14:46
  • Mr Polywhirl's answer is probably the easiest thing. If you're iterating from 0 to `length` with `<`, then the index expression has to also subtract 1, because there's no element at `array[length - 0]`. – Pointy Jan 14 '22 at 14:46
  • 1
    Personally, I'd reverse the looping: `for (let i = array.length - 1; i >= 0 ; i--) { reversedArray.push(array[i]); }` – Andrew Corrigan Jan 14 '22 at 14:49

1 Answers1

3

If you loop backwards, you do not need to perform any index subtraction.

const reversedArray = (array) => {
  const result = [];
  for (let i = array.length - 1; i >= 0; i--) {
    result.push(array[i]);
  }
  return result;
};

console.log(reversedArray([1, 2, 3, 4, 5]));
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • 2
    Or, if we're going down the ES6 route, `reverse = (arr) => arr.map(arr.pop, [... arr]);` ;) – BenM Jan 14 '22 at 14:47
  • 1
    @BenM I tried to honor the for-loop as much as possible. I just rewrote the function definition as an assigned const. :p – Mr. Polywhirl Jan 14 '22 at 14:50
  • Many thanks! Loopping backwards seems cool and that's a nice approach. I've learned something new today and for that I am thankful... – Diego Jan 14 '22 at 14:51