I've created an array that I now want to reverse. The array should display every number from the number specified down to 0.
I've tried using the reverse()
method in various ways but it either misses out every other number in the array or returns nothing at all?
In the code below the else if
statement is where the array wants reversing.
Is there a way to use the reverse()
method on the array here or an alternative way to do this?
Thanks
function myFunc() {
var x = Math.floor(Math.random() * 100);
var counter = [];
var start = 0;
if (x > 40) {
start = 40;
} else {
start = 0; }
for (var i = start; i < x; i++) {
if (x > 40 && i % 2 == 1) {
counter.push(i);
} else if (x < 40) {
counter.push(i).reverse(); //reverse here returns nothing
counter.reverse(); //reverse here returns every other number only
}
}
return counter + ',' + x;
}
console.log(myFunc())