Nearest Lucky Number

CL #rekt

by Jia Nelson

HTML

<h1>
Enter a positive integer and we will find the nearest lucky number
</h1>

<input id="stuff" type="number"/>
<button onclick="nearestLN()">Submit!</button>
<div id="answer"></div>

JavaScript

var counter = 2;

function nearestLN(){
	if(isLucky(parseInt(document.getElementById("stuff").value)))
  	document.getElementById("answer").innerHTML = parseInt(document.getElementById("stuff").value) + " is a lucky no";
   else
   	 document.getElementById("answer").innerHTML = parseInt(document.getElementById("stuff").value) + " is a not a lucky no";
}

function isLucky(n){
	var next = n;
  if(counter > n)
  	return true;
   if(n%counter === 0)
   	return false;
    
   next -= next/counter;
   counter++;
   return isLucky(next);
}