diff --git a/solutions/59.js b/solutions/59.js index 5e29877..43cdda7 100644 --- a/solutions/59.js +++ b/solutions/59.js @@ -3,6 +3,8 @@ // output: false // Solution by Colin Xie @ColinX13 +// Solution1 by Maricris Bonzo @Seemcat + /** * Returns true or false based on whether the number is prime or not prime * @param {number} num - a number that is being input @@ -19,6 +21,20 @@ const solution = (num) => { } return true; }; + +const solution1 = (num) => { + if(num === 1 || num === 0 || num === -1){ + return false; + } + for(let i = 2; i < Math.sqrt(num); i++){ + if(num%i === 0){ + return false; + } + } + return true; +}; + module.exports = { solution, + solution1 }; diff --git a/test/59.js b/test/59.js index 126ec12..214d367 100644 --- a/test/59.js +++ b/test/59.js @@ -1,5 +1,6 @@ const expect = require('chai').expect; let solution = require('../solutions/59').solution; +let solution1 = require('../solutions/59').solution1; // solution = require('./yourSolution').solution; describe('prime numbers', () => { @@ -18,5 +19,21 @@ describe('prime numbers', () => { it('15 should not be a prime number', () => { expect(solution(15)).eql(false); }); + it('1 should not be a prime number', () => { + expect(solution1(1)).eql(false); + }); + it('13 is a prime number', () => { + expect(solution1(13)).eql(true); + }); + it('-1 should not be a prime number', () => { + expect(solution1(-1)).eql(false); + }); + it('0 should not be a prime number', () => { + expect(solution1(0)).eql(false); + }); + it('15 should not be a prime number', () => { + expect(solution1(15)).eql(false); + }); + });