最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Check whether a number is divisible by another number If not make it divisable in Javascript - Stack Overflow

programmeradmin6浏览0评论

I want to check whether a number (7) is divisible by another number (5), If the number is divisible by the number then I need to return the same number. If the number is not divisible by another number I need to make it divisible and return the updated value.

var i =7;
if (i % 5 == 0) {
 alert("divisible by 5");
} else {
    alert("divisible not by 5");
} 

Here if the condition satisfy then I need to return the same value. If the condition is not satisfied I need to add the required number and make it next divisible. (Like 7 is not divisible by 5 so I need add 3 to 7 and return the value 10 which is divisible by 5).

Are there any Math functions exists to implement the same?

I want to check whether a number (7) is divisible by another number (5), If the number is divisible by the number then I need to return the same number. If the number is not divisible by another number I need to make it divisible and return the updated value.

var i =7;
if (i % 5 == 0) {
 alert("divisible by 5");
} else {
    alert("divisible not by 5");
} 

Here if the condition satisfy then I need to return the same value. If the condition is not satisfied I need to add the required number and make it next divisible. (Like 7 is not divisible by 5 so I need add 3 to 7 and return the value 10 which is divisible by 5).

Are there any Math functions exists to implement the same?

Share Improve this question edited Feb 24, 2016 at 15:45 CollinD 7,5732 gold badges24 silver badges47 bronze badges asked Feb 24, 2016 at 15:39 VinodVinod 2,31111 gold badges61 silver badges105 bronze badges 5
  • 4 Java has nothing to do with JavaScript – epascarello Commented Feb 24, 2016 at 15:40
  • Sounds like you need recursion. – epascarello Commented Feb 24, 2016 at 15:40
  • @epascarello why recursion? – tucuxi Commented Feb 24, 2016 at 15:48
  • @tucuxi How else will the OP be able to calculate the value or a loop, or a math function. :) – epascarello Commented Feb 24, 2016 at 15:48
  • @epascarello I see 7 answers as I write this, none of which uses recursion or loops to calculate values. Also, there is nothing that you can do with recursion that you cannot do without it (sometimes with help from a stack). – tucuxi Commented Feb 24, 2016 at 15:51
Add a ment  | 

7 Answers 7

Reset to default 6

What you want, it seems like, is this:

function roundTo(n, d) {
  return Math.floor((n + d - 1) / d) * d;
}

For n 10 and d 5, you get 10 back. For n 7 and d 5, you also get 10. What the code does is add one less than the divisor to the input number, and then divides by the divisor. It then gets rid of any fractional part (Math.floor()) and multiplies by the divisor again.

You can do that by using a simple while loop:

function doThat(number, divider) {
    while(number % divider !== 0) {
      number++;
    }
    return number;
}

doThat(12, 5); // => returns 15

Here's a fiddle: https://jsfiddle/rdko8dmb/

You could use this algorithm:

i % n = r, where i = 7, n = 5, and r = 2.

Then, make i = i + (n - r). i.e. i = 7 + (5-2)10. Then you can use this for your division.

Try this

function divisible(dividend, divisor){
  if (dividend % divisor == 0) {
    return dividend;
  } else {
    var num = dividend + (divisor-(dividend % divisor));
    return num;
  }
}

var res = divisible(7, 5);
console.log(res);

Here is the fastest and cleanest way to do that:

function shift(number, divider) 
{
    return number + (divider - (number % divider)) % divider;
}

It takes number and moves it up by the difference from (our unit) divider to the remainder to make it divisible by the divider. The % divider makes sure that if there is no difference number doesn't get pushed up by a full unit of the divider.

I do not know if this will work for all numbers... But you might try this 7 % 5 = 2. If you subtract 2 from 5 you will get 3... This is the number you need to add to 7. 16 % 5 = 1 subtract 1 from 5 = 4 .. 4 + 16 = 20
Another example 16 % 13 = 3 ... 13-3 = 10 16+10 = 26 26/13 = 2

Here's an example of function that finds next higher natural number that is divisble by y

https://www.jschallenger./javascript-basics/find-next-higher-number

Option 1

while (x % y !== 0) x++;
return x;
}

Option 2

if(x % y == 0){

return x;
}

else{
for(i = x+1; i > x; i++){
if(i % y == 0){
return i; 
}

}

}
发布评论

评论列表(0)

  1. 暂无评论