-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path43-primeChecker.js
More file actions
46 lines (41 loc) · 1.47 KB
/
43-primeChecker.js
File metadata and controls
46 lines (41 loc) · 1.47 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
// STRATEGY
// This is basically a hodge-podge of answers to previous coderbyte exercises (PrimeTime and PermutationStep).
// Chances are, if you've gotten this far into the exercises, the concept behind this one should be fairly easy to grasp.
// First, I took the first part of the PermutationStep function - the part that gives me an array of all possible permutations of an inputted number.
// Then I took the PrimeTime function, which returned true if an input was a prime number, and false if not.
// Using a for loop, I run through each element in the result array (containing the permutations), passing each element thru the PrimeTime function.
// If the PrimeTime function returns true; then we have a prime number and we return 1, if not return 0. Tah-dah.
function PrimeChecker(num) {
var result = [];
var perm = function(current, set){
if (set.length == 0){
result.push(parseInt(current));
return;
}
for (var i = 0; i < set.length; i++){
var pivot = set[i];
set.splice(i, 1);
perm(current + pivot, set);
set.splice(i, 0, pivot);
}
}
perm('', num.toString().split(''));
var primeTime = function(numero){
if (numero == 1){
return false;
} else{
for (var j = 2; j < numero/2; j++){
if (numero % 2 === 0){
return false;
}
}
return true;
}
}
for (var k = 0; k < result.length; k++){
if (primeTime(result[k]) === true){
return 1;
}
}
return 0;
}