In this JavaScript code finds count of the numbers divisible by ‘m’ in a given range
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <!-- count the numbers divisible by ‘m’ in a given range --> <input type="text" id="numFrom" placeholder="Enter (From) number"> <input type="text" id="numTo" placeholder="Enter (TO) number"> <input type="text" id="m" placeholder="Enter (M) number"> <input type="button" value="Calculate" id="calculate"> <div id="result" style="width:150px; border: 1px solid #ddd;position: absolute;top:50px;left:50;padding:20px"> </div> <script> function calulate() { let counter=0; let numFrom=document.getElementById("numFrom").value; let numTo=document.getElementById("numTo").value; let numDivider=document.getElementById("m").value; let result=document.getElementById("result"); numFrom=Number(numFrom); numTo=Number(numTo); numDivider=Number(numDivider); for(let i=numFrom;i<=numTo;i++) { // if iteration number divisible to numDivider, block works. if(i%numDivider==0) { counter++; //counting divisible numbers by M } } result.innerHTML=`${counter} numbers are divisible by ${numDivider} in the given range`; } var calculateBtn=document.getElementById("calculate"); calculateBtn.onclick=calulate; </script> |