-3

const arr = [1, 2, 3, 4, 5, 6, 7];

function rev(arr) {
  var value;
  var nArr = [];
  for (let i = arr.length - 1; i >= 0; i--) {
    value = arr[i];
    for (let j = 0; j < arr.length; j++) {
      nArr[j] = value;
    }
  }
  console.log(nArr);
}

rev(arr);

I thought this would work but i keep getting an output of the first index in the test array which means one, it's not reversed at all and two, the new array is outputed [1,1,1,1,1,1,1]

MikeM
  • 13,156
  • 2
  • 34
  • 47

1 Answers1

0

The problem here is that you have nested for loops, you take value from index of the outside loop and replace entire array with that value with inner loop, then continue to the next index of the ouside loop, so at the end you end up with the value from the last index of the outside loop. What you need is one loop, and push() value into new array:

const arr = [1, 2, 3, 4, 5, 6, 7];

function rev(arr) {
  var value;
  var nArr = [];
  for (let i = arr.length - 1; i >= 0; i--) {
    value = arr[i];
    nArr.push(value);
  }
  console.log(nArr);
}

rev(arr);

Note, this is very inefficient method to achieve that, the answer from @dippas would be a better choice if you need the result, not the journey ;)

[EDIT]

To better understand what's happening in your original code use console.log():

const arr = [1, 2, 3, 4, 5, 6, 7];

function rev(arr) {
  var value;
  var nArr = [];
  for (let i = arr.length - 1; i >= 0; i--) {
    value = arr[i];
    for (let j = 0; j < arr.length; j++) {
      nArr[j] = value;
      console.log("i:", i, "j:", j, "value:", value, "nArr:", "[" + nArr + "]");
    }
  }
  console.log("result:", "[" + nArr + "]");
}

rev(arr);
vanowm
  • 9,466
  • 2
  • 21
  • 37
  • So the nested loops are the problem huh I think I only understand slightly what you mean if I'm being honest. It's confusing to me why the value being repeated is 1 when i have the value of i starting as arr.length-1 in the first loop. I would at least think the repetition would be all 7's sorry for not understanding your explantion but I thank you very much for the correct method. I guess I should study the array properties more! – Andre H Jun 19 '22 at 23:21
  • Well, it is going through all the values as you expected, but inner loop replaces EVERY item in the new array with the the same value. And when outer loop reaches beginning of the original array, that last value (which is `1`) replaces all the items in the new array – vanowm Jun 19 '22 at 23:28
  • Ooooh okay I understand now, so the value variable is basically holidng onto 1 because thats the last value to be passed in the first loop and then the second loop executes and inputs that value into the new loop repeatedly until it's done. I'm dumb :( I thouhgt my first loop would iterate through the array and then the second loop would take that value and input it into the new array smh. Thanks again ! – Andre H Jun 19 '22 at 23:35
  • Thanks for the console.log() suggestion, that is a very useful tip much appreciated! So my code is for some reason taking each index in the array and putting it into the new array over and over again until it finally reaches the last one. its a little confusing but I'll look through it and try to understand what's going on. – Andre H Jun 20 '22 at 00:03