Show how many times input N can be divided by input M

Here you have a snippet with the answer (it’s really simple, it is not how a good script should be done), just run it to see how it works if you can’t get over your homework:

document.querySelector("button").addEventListener("click", ()=> {
var inputN = document.querySelectorAll("input")[0].value;
var inputM = document.querySelectorAll("input")[1].value;

if (isNaN(inputN) || isNaN(inputM)) {
	document.querySelector("#text").innerHTML= "Your input is not a number!";

} else {
	var result = 0;
  while ((inputN % inputM) == 0 && inputN > 0) {
  	result++;
    inputN = inputN - inputM;
  }
  document.querySelector("#text").innerHTML= "You can divide the numbers " + result + " times";
}

})
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Document</title>
   </head>
   <body>
      <div id=inputn> <input type="text"> </div>
      <div id=inputm> <input type="text"> </div>
      <div id=button><button>submit</button> </div>
      <div id=text>
         <p></p>
      </div>
   </body>
   <script></script>
</html>

Leave a Comment