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]

[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]);

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.

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.

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

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.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.