-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_first_prime_above.js
More file actions
57 lines (43 loc) · 1.29 KB
/
08_first_prime_above.js
File metadata and controls
57 lines (43 loc) · 1.29 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
57
/*
Write a function that returns the first prime number above given number
Examples:
firstPrimeAbove(3) => 5
firstPrimeAbove(0) => 2
firstPrimeAbove(15) => 17
**Your function must return a value**
It's not necessary to print the result on screen,
however to test your function you are free to print the result
*/
function firstPrimeAbove(number) {
for (let nextPrimeCandidate = number + 1; nextPrimeCandidate > 0;nextPrimeCandidate++) {
let index = 2;
while (nextPrimeCandidate % index !== 0 && index < nextPrimeCandidate) {
index++;
}
if (nextPrimeCandidate === index) {
return index;
}
}
}
function getMark(isPassed) {
return isPassed ? '✅' : '❌';
}
function getMessage(number, actual, expected) {
let message = "Number :" + number + "\n Expexted : " + expected;
message += " Result => " + actual;
return message;
}
function testFirstPrimeAbove(number, expected) {
const actual = firstPrimeAbove(number);
const isPassed = actual === expected;
console.log(getMark(isPassed), getMessage(number, actual, expected));
}
function tests() {
testFirstPrimeAbove(0, 2);
testFirstPrimeAbove(3, 5);
testFirstPrimeAbove(15, 17);
testFirstPrimeAbove(16, 17);
testFirstPrimeAbove(1, 2);
testFirstPrimeAbove(11, 13);
}
// tests();