문제
풀이 1
function solution(nums) {
var answer = 0;
function primeCheck(num) {
for (let i=2; i<=Math.sqrt(num); i++){
if (num % i===0) return false;
}
return true;
};
function getCombinations(arr, selectNum) {
const results = [];
if (selectNum === 1) return arr.map(v => [v]);
arr.forEach( (fixed, index, origin ) => {
const rest = origin.slice(index+1);
const combinations = getCombinations(rest, selectNum-1);
const attached = combinations.map( (combination) => [fixed, ...combination]);
results.push(...attached)
});
return results;
};
getCombinations(nums,3)
.map( v => v.reduce((a,b) => a+b))
.map(v => {
if(primeCheck(v)) return answer++
});
return answer;
}