0

There's a way I can "invert" my array of prime numbers? or I have to do in other logic way.

There's my code

var primos = [];
for (var i = 0; i <= 100; i++) {
    var fake = false;
    for (var j = 2; j <= i; j++) {
        if (i%j==0 && j!=i) {
            fake = true;
        }
    }
    if (fake === false) {
        primos.push(i);
    }
}
for(i = primos.length; i > 0; i--) {
    console.log(primos[i]);
}

enter image description here

I want the result to be the array but in descending order Like [97, 89, 83, ...]

Pedro Sturmer
  • 547
  • 6
  • 20

1 Answers1

1

If you want to simply revert the result you can use the Array prototype method reverse:

var primos = [];
for (var i = 0; i <= 100; i++) {
    var fake = false;
    for (var j = 2; j <= i; j++) {
        if (i%j==0 && j!=i) {
            fake = true;
        }
    }
    if (fake === false) {
        primos.push(i);
    }
}
primos = primos.reverse();
for(i = primos.length; i > 0; i--) {
    console.log(primos[i]);
}

Take a look at this other answer for some ideas on other ways to accomplish this:

How to find prime numbers between 0 - 100?

Vinicius Santana
  • 3,936
  • 3
  • 25
  • 42