html
html
1
2
3
4
5
6
7
8
9
<!DOCTYPE html>
<html>
<head>
<title>Greater Common Divisor</title>
</head>
<body>
<!-- see JS tab -->
</body>
</html>
Enter to Rename, Shift+Enter to Preview
css
css
1
2
3
body {
}
Enter to Rename, Shift+Enter to Preview
js
js
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
/*
( Sorry for be late, but real life was called me ^^ )
This is not as bad for a first step ;)
Some remarks:
> your idea to test if user entry is valid is good, but we'll see later how to improve this task...
> what are you expected with the line 'debugger;'?
> your primeNum() basis seems good, but don't implement what I'm meaning by storing all primes in an array :P
> your final 'if' statement only test if an unique value is prime, and store only once in case of prime number... not at all decompose user number entry... ( as well as you need a second user number entry to calculate GCD of both ^^
Let define the primeNum() function outside of your first 'if' statement ( and anyway fix the missing closing parenthesis -- rounded bracket ), with some fix, improvement and implementation of array to store primes:
*/
var primes = [2]; // prefer explicit name for readability than single letter name almost the time, and especially for global variables ^^
// we need later to have at least one prime prefilled (you can also prefill how many you want, just be sure to put all and only primes, in ascending order)
function primeNum(num) {
// inside the function you need to use the parameter variable, not the global one
if ((!num)||(num==1)) { // 0 is equivalent to false, so if num==0 is equivalent to if not num==0 as well as if not num
return false; // 0 and 1 case are not considered as primes
}
if (primes.indexOf(num)!=-1) {
return true; // num is prime already stored
}
var s = Math.sqrt(num); // Math.round() is unnecessary
Enter to Rename, Shift+Enter to Preview
BROWSER
Console
Run