Last element of an array showing different results

Hello, I was trying to denote the last element of an array but I find it pretty confusing. Let me show you.
what you can see here is getting the last element of an array.

But there is a twist. I was applying this very concept to write a reverse array . see, what I got :worried:

I am going to give you the code so can test it and figure out the problem.

var reversearr = function(arr){
  
  function swap(arr, start, end){
    while(start<end){
      [arr[start], arr[end]] = [arr[end], arr[start]];
      start++;
      end--;
    }
  }
  swap (arr, arr[0], arr[arr.length-1]);
  return arr;
}

var arr = [1,2,3,4,5,6,7,8,9];
console.log(reversearr(arr));

Could you please describe this? I am really struggling :face_exhaling:

Is this an fcc challenge? Can you post a link to the problem description?

Hi, it’s not an fcc challenge. Rather I took the concept from this website and been applying this algorithm for array reversal. It seems I am on the verge of success but… I am stuck !

so is the idea to swap the order only or to swap based on value?
for eg. if you have [3, 4, 2] should you get [2, 4, 3]

The problem is you are passing numbers in the array for start and end and then are assuming those match up with indices in the array.

You can see what happens when making the following call:

var arr = [101, 102, 103, 104, 105, 106, 107, 108, 109];
console.log(reversearr(arr));

The bottom line is your function should not care what the values of the array at any position. You only would need to pass the array to swap and then specify indices instead of values for start and end variables local to the swap function.

Your swap function will look more like:

function swap(arr) {
    let start = // applicable start index
    let end = // applicable ending index
    while (start < end) {
      [arr[start], arr[end]] = [arr[end], arr[start]];
      start++;
      end--;
    }
  }

I am not really sure why you even have a swap function declared inside the reversearr function. Since the logic is only used once, I would move the logic out of the swap function, put it into the reversearr function and get rid of the swap function all together.

[8,7,2,5,4,3], it should return [3,4,5,2,7,8]. just reverse the order.

ah okay. why not just read the array in reverse with a for loop?

1 Like

As a novice programmer, I find it difficult. could you please show me what the indices should be? I feel that the problem lies in right here…
swap (arr, arr[0], arr[arr.length-1]);

In the code above, you were already referencing the indices. Use those for the variables start and end. Make sure you understand the difference between an array index and an array element. The code above you were passing elements for start and end but you did specify the correct indices to get those elements. Those indices are what you should use for the local variables inside the function.

well, I was just figuring out in what ways can I write a program for this array reversal. It seems this method was never used to reverse an array. May be I am wrong. But you see, it was working.

update: sorry, not working.

What was working? The function you wrote did not reverse the array.

Well maybe you can explore this method if you like but first make sure you know how to do it the normal way. That is, a simple for loop and write the values into a new array in reverse.

If you get that working, the. Try to use other methods like swapping.

I tried the way you said…but it’s not working. could you please share the code? I mean is it possible to reverse an array using this method? If not then I’ll quit.

Show us what you tried.

function reversearr(arr) {
  
function swap(arr) {
    while (start < end) {
    
    let start =arr[0]; // applicable start index
    let end = arr[arr.length-1]; // applicable ending index
      
      [arr[start], arr[end]] = [arr[end], arr[start]];
      start++;
      end--;
    }
  swap (arr, start, end);
    return arr;
};   
 }
var arr = [9,4,2,1,6,8,7]; 
console.log(reversearr(arr));

just gonna give you my 2 cents and be gone…
I strongly suggest that you go back and look at the fCC javascript course if you haven’t already.
Understanding some fundamentals first of coding will greatly help you.

1 Like

You have not assigned an index value to start or end. I am a little confused how you are using a more advanced destructuring technique to swap the values for the variables but appear to not understand the difference between an array index and an array element.

An array is a data structure which represents a collection of items stored at contiguous memory locations.
Elements in an array are referenced by an index starting from zero (depending on the language)

arr = [1, 2, 3]

the first element is referenced by a[0], that is element at index 0, second by a[1], that is element at index 1, in that order.
so start and end should denote the start and end indexes in the array you want to reverse.
you can either pass them as arguments to the function like you did initially or set them to default to the index, (0) and index (arr.length - 1).
I hope this finds you well.