-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge_46_test.js
More file actions
56 lines (49 loc) · 1.02 KB
/
Copy pathchallenge_46_test.js
File metadata and controls
56 lines (49 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
Challenge 46; Primes < N
*/
var expect = require('chai').expect;
function isPrime(n) {
var i = 2
, sqrt_n = Math.floor(Math.sqrt(n))
;
while (i <= sqrt_n) {
if (n % i === 0 ) {
return false;
}
i += 1;
}
return true;
}
function getPrimes(N) {
var primes = []
;
if (N <= 2) {
return [];
} else {
primes.push(2);
for(var i = 3; i < N; i+= 1) {
if (isPrime(i) ) {
primes.push(i);
}
}
}
return primes.join(',');
}
describe('isPrime()', function() {
it('should be true', function() {
expect( isPrime(11) ).to.be.ok;
});
it('should be false', function() {
expect( isPrime(100) ).to.not.be.ok;
});
it('should be false', function() {
expect( isPrime(4) ).to.not.be.ok;
});
});
describe('getPrimes()', function(){
it('should be "2,3,5,7"', function(){
expect( getPrimes(10) ).to.be.equal("2,3,5,7");
});it('should be "2,3,5,7,11,13,17,19"', function(){
expect( getPrimes(20) ).to.be.equal("2,3,5,7,11,13,17,19");
});
});