हमें एक जावास्क्रिप्ट फ़ंक्शन लिखना है जो एक संख्या लेता है और पहला अभाज्य संख्या देता है जो 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