assuming that an input was not equal to 1 or 2 (e.g. an input of 15), it would go through the loop, return false, but wouldn't that return value be overridden by the 'return true' underneath it that's outside of the for loop?
help to understand this would be much appreciated.
function checkIfPrime(numb) {
if (numb === 1) {
return false;
} else if (numb === 2) {
return true;
} else {
for (let x = 2; x < numb; x++) {
if (numb % x === 0) {
return false;
}
}
return true;
}
}
console.log(checkIfPrime(2));
console.log(checkIfPrime(15));
console.log(checkIfPrime(17));
console.log(checkIfPrime(19));
Aucun commentaire:
Enregistrer un commentaire