DEV Community

Discussion on: Monday Express BigO Notation [Day 1]

Collapse
 
claudwatari95 profile image
ClaudWatari • Edited

Here, we start by checking if the number is equal to 1, since we know 1 is not a prime number, we return and exit from the function.
We then loop from integer 2 and exit at integer n and check if current value of n is divisible by current iterator value, which means it is not a prime number. Otherwise, it is a prime number.
Of course, we started off by ensuring that n is an integer, by checking it's data type.

EDIT: **Cannot figure out why screenshot is not uploading, so I've pasted my code instead.

const primality= (n) => {
if(typeof n !== 'number') return 'Must be a number'
if(n === 1) return 'Not prime'
for(var x = 2; x < n; x++)
if(n % x === 0) return 'Not prime';
return 'prime';
};

console.log(primality(3));
console.log(primality(12));
console.log(primality(5));
console.log(primality(7));