skip to Main Content

i expect the elements in arr be reversed, but it still log as [‘a’,’b’,’c’,’d’,’e’]

i think i am doing some fundamental problem but i cannot figure out what it is

    function reverseArray(array) {
       let array02 = [];
       let len = array.length-1;
       for (let i=0; i<=len; i++){
           array02[len-i] = array[i];
       }
    array = array02;
    return array;
    }
    var arr = ['a','b','c','d','e'];
    reverseArray(arr);
    console.log(arr);

4

Answers


  1. Yes, the fundamental point you’re missing is that you’re doing basically this:

    function reverseArray(array) {
        array = ....some new data
    }
    
    var arr = ...something
    reverseArray(arr)
    

    and expect the variable arr to change its value. This doesn’t work in Javascript, because Javascript passes arguments by value and any assignment to a function argument (array = ....) cannot affect a variable in the calling code.

    What you need here is either return a new value and assign it in the caller code

    function reverseArray(array) {
        return ....some new data
    }
    
    var arr = ...something
    var newArr = reverseArray(arr)
    

    or change the value itself, and not the variable:

    function reverseArray(array) {
        array[index] =  ....some new data
    }
    
    var arr = ...something
    reverseArray(arr)
    
    Login or Signup to reply.
  2. You can modify the function to directly reverse the elements of the input array without creating a new array like this:-

    function reverseArray(array) {
       let len = array.length - 1;
       for (let i = 0; i <= len / 2; i++) {
           // Swap elements at positions i and len - i
           let temp = array[i];
           array[i] = array[len - i];
           array[len - i] = temp;
       }
    }
    
    var arr = ['a', 'b', 'c', 'd', 'e'];
    reverseArray(arr);
    console.log(arr);
    
    Login or Signup to reply.
  3. You are re-assign array to array02 inside the function. That is why main array is lost. Try this one for reversing array.

    You can also use array.reverse function.

    function reverseArray(array) {
        array.reverse();
        return array;
    }
    
    var arr = ['a', 'b', 'c', 'd', 'e'];
    reverseArray(arr);
    console.log(arr);
    

    Or like this way.

    function reverseArray(array) {
        let lenth = array.length - 1;
        for (let i = 0; i <= lenth / 2; i++) {
            let tmp = array[i];
            array[i] = array[lenth - i];
            array[lenth - i] = tmp;
        }
        return array;
    }
    
    var arr = ['a', 'b', 'c', 'd', 'e'];
    reverseArray(arr);
    console.log(arr);
    
    Login or Signup to reply.
  4. What you are doing is assigning array1 to array2 inside reverseArray(array) function. By doing so you are losing the original array.

    function reverseArray(array) {
       let len = array.length - 1;
       for (let i = 0; i <= len / 2; i++) {           
           let temp = array[i]; // Swap elements using temp variable
           array[i] = array[len - i];
           array[len - i] = temp;
       }
    }
    
    var arr = ['a', 'b', 'c', 'd', 'e'];
    console.log(JSON.stringify(arr))
    reverseArray(arr);
    console.log(JSON.stringify(arr));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search