1

I try to reverse an array but without the reverse function.

Like you see I try to decrement the length of the array num=(tab.length-i)-1, so it works. But when I try to add tab[i]=tab[num], it works but the value push inside tab[i] is random [ 10, 9, 8, 7, 6, 6, 7, 8, 9, 10 ].

Do you have any idea why ?

const tab=[1,2,3,4,5,6,7,8,9,10]

let num=0

for(let i=0;i<tab.length;i++){
    num=(tab.length-i)-1
    console.log(num)
    tab[i]=tab[num]
}
3
  • 1
    You're overwriting the values without saving them, so the array ends up symmetrical around the center point Commented Sep 25, 2019 at 9:07
  • Why you don't use a temp array as buffer ? Commented Sep 25, 2019 at 9:09
  • Why avoid the reverse? You could clone the array first if you want to avoid changing the original array. E.g. tab.slice(0).reverse(). Commented Sep 25, 2019 at 9:29

3 Answers 3

2

the problem is you're modifying the array at the same time you look into it, so when you get to the middle element it was already modified.

here's a manual execution of your script :

i | tab
0 | [1,2,3,4,5,6,7,8,9,10]
1 | [10,2,3,4,5,6,7,8,9,10]
2 | [10,9,3,4,5,6,7,8,9,10]
3 | [10,9,8,4,5,6,7,8,9,10]
4 | [10,9,8,7,5,6,7,8,9,10]
5 | [10,9,8,7,6,6,7,8,9,10]
6 | [10,9,8,7,6,6,7,8,9,10]
7 | ...
8 | ...
9 | ...

I think you can see the problem there

there are 2 ways to solve this problem:

// either create a temp array to keep previous values
const reverseWithTmpArray = arr => {
  let tmp = [...arr]

  for(let i = 0; i < arr.length; i++){
    let num = (arr.length - i) - 1
    arr[i] = tmp[num]
  }  

  return arr
}

// or reverse the element by pairs 
const reverseByPairs = arr => {
  for(let i = 0; i < arr.length / 2; i++){
    let num = (arr.length - i) - 1

    let tmp = arr[i]
    arr[i] = arr[num]
    arr[num] = tmp

    // can also be written
    // [arr[i], arr[num]] = [arr[num], arr[i]]
    // using destructuring assignement
  }

  return arr
}

console.log(reverseWithTmpArray([1,2,3,4,5,6,7,8,9,10]))
console.log(reverseByPairs([1,2,3,4,5,6,7,8,9,10]))

As I wrote them these 2 function mutate the input array, it can be rewritten to prevent this side effect

doc of destructuring assignement

Sign up to request clarification or add additional context in comments.

Comments

0

You have to use a temporary variable. Without that, you are just overwriting values like so.

Step 01: 1,2,3,4,5,6,7,8,9,10
Step 02: 10,2,3,4,5,6,7,8,9,10
Step 03: 10,9,3,4,5,6,7,8,9,10
Step 04: 10,9,8,4,5,6,7,8,9,10
Step 05: 10,9,8,7,5,6,7,8,9,10
Step 06: 10,9,8,7,6,6,7,8,9,10

// Now it will just take the values, but they are the same, so you are left with a mirrored array.

Step 07: 10,9,8,7,6,6,7,8,9,10
Step 08: 10,9,8,7,6,6,7,8,9,10
Step 09: 10,9,8,7,6,6,7,8,9,10
Step 10: 10,9,8,7,6,6,7,8,9,10

Do it like this instead:

let tab = [1,2,3,4,5,6,7,8,9,10],
    temp = []; // Create temp

let num=0

for(let i=0;i<tab.length;i++){
    num=(tab.length-i)-1
    temp[i]=tab[num] // Add to temp
}

tab = temp; // Assign temp to tab

console.log(tab)

Comments

0

var tab = [1,2,3,4,5,6,7,8,9,10, 11];

console.log("Before reverse:", tab);

for (var k = 0; k < parseInt(tab.length/2); k++) {
    var reverseIndex = (tab.length - 1) - k;
    var tmp = tab[reverseIndex];
    tab[reverseIndex] = tab[k];
    tab[k] = tmp; 
}

console.log("After reverse:", tab);

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.