हमें एक JavaScript फ़ंक्शन लिखना है जो दो संख्याएँ, a और b, लेता है और a और b के बीच आने वाली सभी अभाज्य संख्याओं का योग लौटाता है। यदि a और b अभाज्य हैं तो उन्हें भी शामिल करना चाहिए।
कोड
const num1 = 45; const num2 = 345;
const isPrime = n => {
if (n === 1) { return false; }
else if (n === 2) { return true; }
else {
for (let x = 2; x < n; x++) {
if (n % x === 0) { return false; }
}
return true;
}
};
const primeBetween = (a, b) => {
const res = [];
while (a <= b) {
if (isPrime(a)) { res.push(a); }
a++;
}
return res;
};
console.log(primeBetween(num1, num2));
आउटपुट
[ 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337 ]