Home > Mobile >  Write a function definition named quotient that takes in two numbers and returns the quotient of div
Write a function definition named quotient that takes in two numbers and returns the quotient of div

Time:01-24

I am taking an assessment for JavaScript and I am stuck. I have tried tweaking and moving things around with no luck. Basically it is a series of questions that once answered correctly moves onto the next one. For this one there is this bit of info beneath the question. this is what I have which is giving back expected: 2 & instead got: 2.5 in my console.

The question is to: Write a function definition named quotient that takes in two numbers and returns the quotient of dividing the first argument by the second argument.

function quotient(x, x1) { return x = x / x1; }

CodePudding user response:

an instructor responded with:

Just doing straight division like 5 / 2 will return the quotient AND the remainder as a decimal number. In this case: 2.5
The problem ONLY wants the quotient (i.e., the whole number part) and NOT the remainder. In this case: 2 (or 2.0 should also be fine)

With this clarification, it turns out they are looking for euclidian integer division. You can achieve this by using Math.floor on the result of the normal division:

function quotient(dividend, divisor) {
  var x = dividend / divisor;
  return Math.floor(x);
}

CodePudding user response:

I've finally got the answer. Thanks for helping!!

function quotient(x, y){
  return Math.floor(x / y);
}
  • Related