1
小算法练习
source link: https://toweave.github.io/javascript/2019-04-28-practise/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
小算法练习
Sunday, April 28th 2019, 12:36:46 pm ( ISO 8601 )
let a = ''
function testA(n, i=2) {
if (n % i === 0 && i <= n) {
console.log('ii:', i)
a += '*' + i
testA(n / i, i)
} else if (n % i !== 0 && i <= n) {
i++;
testA(n, i)
}
}
testA(24)
console.log(a)
// ii: 2
// ii: 2
// ii: 2
// ii: 3
// *2*2*2*3
function isPrime(element, index, array) {
let start = 2;
while (start <= Math.sqrt(element)) {
if (element % start++ < 1) {
return false;
}
}
return element > 1;
}
console.log([4, 6, 8, 12].find(isPrime)); // undefined, not found
console.log([4, 5, 8, 12].find(isPrime)); // 5
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK