I’ve written a function to iterate through values up to a number you pass along as an argument and then put all of the prime numbers below that number into an array.
console.log(showPrimes(10));
function showPrimes(max) {
let numbers = [];
for (let i = 2; i < max; i++){
let isPrime = true;
for (let testno = 2; testno < i; testno++){
if (i % testno === 0) isPrime = false;
}
console.log(isPrime);
console.log(i);
if (isPrime = true)numbers.push(i);
}
return numbers;
}
I’m currently logging isPrime and each value of i to the console, and it is correctly identifying which numbers are and are not prime but, regardless, -all- values of i are getting put into the array ‘numbers’. I do not know why, but the "if(isPrime === true)"condition simply is not working. The array always returns with all possible values of i regardless of whether isPrime === true.
Annnnd… I think I just saw my mistake.
You know, sometimes all it takes is having to type it out a second time. It should be isPrime === true, not isPrime = true, so it was actually setting isPrime to true… and then inserting it into the array.
So… I answered my own question, but not before posting it unfortunately.