FAQ: Code Challenges: Intermediate JavaScript - reverseArray()

return inside a loop is often one source of problems. return of a .push() expression gives the new length of the array. Give these two considerations some thought and you should be able to rework your code.

3 Likes

Thank you, it makes sense.

1 Like

My solution is not being accepted even though it creates a new array, returns the original array reversed and works with numbers.

this is the code:

// Write your code here:

function reverseArray(newArr) {

    newArr = [];

    for (let i = sentence.length-1; i >= 0; i--){

        newArr.push(sentence[i]);

    }

    return newArr;

}

// When you're ready to test your code, uncomment the below and run:

const sentence = ['sense.','make', 'all', 'will', 'This'];

const sentence2 = [1, 2, 3, 4, 5];

console.log(reverseArray(sentence)) 

// Should print ['This', 'will', 'all', 'make', 'sense.'];

I’ve done it using .reduce().

const reverseArray = array => array.reduce((newArray, element) => {
  newArray.unshift(element);
  return newArray;
}, []);

i just wanna share my solution

const reverseArray = array => {
  let reversed = []
  for(let i = 1; i <= array.length; i++) {
    reversed[i-1] = array[array.length - i ]
  }
  return reversed
}

4 posts were split to a new topic: Solution works but is not accepted

Could anyone explain how this line of code works in detail?

  for(let i = 1; i <= array.length; i++) {
    reversed[i-1] = array[array.length - i ]
  }

specially the second line, thanks.

We know that the signature line sets up iteration of the sequence,

 1, 2, 3, ..., array.length

The second line polls the array element at position, array.length - i. That sequence is,

array.length - 1, array.length - 2, ..., array.length - array.length

The value at that polled position is then inserted into the reversed array at position i - 1,

 0, 1, 2, ... array.length - 1
1 Like

Thank you, makes a lot more sense now. :smiley:

If you were to replace the word ‘sentence’ with any other word, the code would error as it would not be able to find the array because the argument is ‘newArr’, which is created inside the function. This code is targeting a very specific array which is why CodeCademy have considered it incorrect, despite it working.

Using a previous example that @mtf gave:

const reverseArray = obj => {
  reversed = [];
  for (let i = obj.length-1; i>=0; i--) {
    reversed.push(obj[i]);
  }
  return reversed;
}

The argument is not specific, in this case using ‘obj’. This could be replaced with ‘insertArgumentHere’ and it would still work. To use your code within this example would look like this:

const reverseArray = insertArgumentHere => {
  newArr = [];
  for (let i = insertArgumentHere.length-1; i>=0; i--) {
    newArr.push(insertArgumentHere[i]);
  }
  return newArr;
}

In regards to the reverseArray() practice exercise in Intermediate JS - https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/web-dev-intermediate-javascript-practice/lessons/intermediate-javascript-coding-challenge/exercises/reverse-array

Can anyone see why this code isn’t working? I’m sure it’s almost there…

function reverseArray(sentence) {
let newArray = ;
for (let i = sentence.length-1; i>= 0; i–) {
newArray.push(sentence[i])
}
return newArray
}
console.log(newArray);

newArray is local to the function so inaccessible. Try printing the call expression?

print (foo(arg))

Thanks for the prompt response mtf1 Working fine now1

1 Like

I dont know, where I went wrong.

const reverseArray = obj => {
reversed = ;
for (let i = sentence.length-1; i >= 0; i–);
{
reversed.push(obj[i]);
};
return reversed

};
const sentence = [‘sense.’,‘make’, ‘all’, ‘will’, ‘This’];

console.log(reverseArray(sentence))

Put it this way… Where do you think you are on the right track? Review your thinking and logic and see if you cannot point out your own mistake.

Okay, one step closer, but the program isn’t satisfied and I don’t know why. That’s frustrating.

const reverseArray = obj => {
 let reversed = [];
  for (let i = sentence.length-1; i >= 0; i--)
  {
    reversed.push(obj[i])
  }
 return reversed
}
const sentence = ['sense.','make', 'all', 'will', 'This'];

console.log(reverseArray(sentence))

The output:

[ 'This', 'will', 'all', 'make', 'sense.' ]

Why is that wrong?

Should that be obj?

1 Like

My solution:

const reverseArray = (array) =>
  array.map((element, index) => 
    array[array.length - 1 - index]);

const reversed = reverseArray(sentence);

console.log(reversed); 

This returns what was asked, does anyone have suggestions for improvement?
Thanks :wink:

Hey there!
I tried to solve this exercise with a different approach with the .sort method but the result is not as expected. I wanted to use the index of each string to sort them but it won’t work. Here is my code:

const sentence = [‘sense.’,‘make’, ‘all’, ‘will’, ‘This’];

/*
console.log(sentence.indexOf(‘sense.’));

console.log(sentence.indexOf(‘make’));

console.log(sentence.indexOf(‘all’));

console.log(sentence.indexOf(‘will’));

console.log(sentence.indexOf(‘This’));
*/
const reverseArray = array => array.sort ((string1, string2) => array.indexOf(string1) > array.indexOf(string2));

console.log(reverseArray(sentence))

How does .unshift work in this format? Im only reading about unshift moving elements to the beginning of an array. Imagine its moving each element to the beginning one at a time as the loop rolls thru. Just didnt expect the usage of the method within a loop.