हमें एक JavaScript फ़ंक्शन लिखना होगा जो एक संख्या लेता है और n के बाद आने वाली पहली अभाज्य संख्या लौटाता है।
उदाहरण के लिए: यदि संख्या 24 है, तो आउटपुट 29 होना चाहिए।
इसलिए, आइए इस फ़ंक्शन के लिए कोड लिखते हैं:
उदाहरण
इसका कोड निम्नलिखित होगा:
const num = 24;
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 nearestPrime = num => {
while(!isPrime(++num)){};
return num;
};
console.log(nearestPrime(24));
आउटपुट
कंसोल में आउटपुट होगा:
29