Q:

infinite while loop in javascript

My problem question as in the practice course goes as follows:
Write a JavaScript program to create a function which takes 2 integers as inputs.  The function divides the first integer with second integer as long as the result (Quotient) is an integer (i.e. remainder is zero) and return the quotient as result.  Your output code should be in the format console.log("Result is ", variableName)
And below is my code:
var num = prompt("Enter number to divide");
var d = prompt("Enter divisor");

function divide(x, y) {
  var result;
  if (d === 1) {
    result = num;
  } else {
    while (num % d === 0) { //while error
      result = num / d;
    }
  }
  return result;
}
var output = divide(num, d);
console.log("Result is: ", output);

If I remove the while loop, program works fine but the problem description says I have to use it. 
What am I doing wrong? 

A:

There are a few issues:
1) If your function receives the arguments x and y, then use those inside his scope, don't access the global variables. 
2) You are never changing the variables that are evaluated on the while condition, so the evaluation will be the same, ever! 
3) Another good thing you can do is add some validation on the received arguments. 
Now, your code, can be rearranged to this one:

function divide(x, y)
{
    if (isNaN(x) || isNaN(y))
        return "Invalid arguments!"; 

    if (y === 1)
        return x;

    while (x % y === 0)
    {        
        x = x / y;
    }

    return x;
}

var num = prompt("Enter number to divide");
var d = prompt("Enter divisor");
var output = divide(num, d);
console.log("Result is: ", output);

