I like your solution! However I’m kind of confused as to how it works. Doesn’t the forEach method start with index 0 then moves through all the rest? My logic is telling me it should just return the same array that you put in, how is this working in reverse?
Hello, I wanted to share my solution. Any thoughts?
my solution:
function reverseArray(array) {
let newArray = [];
for (const i in array) {
newArray.unshift(array[i]);
}
return newArray
}
I also remembered the method that puts everything at the beginning of the array
const reverseArray = (array) => {
const alteredArr = [];
array.forEach(element =>{
alteredArr.unshift(element);
}
)
return alteredArr;
};
This article greatly helped me - thank you.
I was staring at the spare array with the square brackets, wondering why it was there, and I also didn’t understand why we were using .push, although I think I understand, now…
Hey, can anyone let me know why this solution does not count as a viable answer?
To my understanding, a new array was created and returns the ‘sentence’ array in reverse as requested
Just tossing this out there: Are there any edge cases?
Noticed on line 3 sentence instead of array.
Once that is fixed reverseArray([])
returns []
, which is one edge case. Might be moot, though. Make the fix and test again.