简体   繁体   English

检查数字是否可以被JS中的另一个数字整除

[英]Check if number is divisible by another number in JS

I am pretty new at this stuff, and I am solving a series of questions, but I've got lost in this one.我对这个东西很陌生,我正在解决一系列问题,但我迷失在这个问题上。

I have to verify if a number can be divided by other, and the answer must be true or false.我必须验证一个数字是否可以除以其他数字,并且答案必须是对或错。

I got his我得到了他的

function solucao(numero, x) {
    
    solucao(numero % x); 
    
    if (solucao === 0)  {
        resultado = true;
        
    }else{
        resultado = false;
    }
            
}

But I'm getting runtime error and can't see whats is missing.但是我遇到了运行时错误并且看不到缺少什么。

So you want to check if a number numero is divisible by x .所以你想检查一个数字numero是否可以被x整除。 The modulo operator can help.模运算符可以提供帮助。 Try this:尝试这个:

function solucao(numero, x){
    if (numero % x == 0){
        return true
    }
    else {
        return false
    }
}

function solucao(numero, x) {
    
    let resultado;
    
    if (numero % x === 0)  {
        resultado = true;
        
    }else{
        resultado = false;
    }
            return resultado;
}

I think you get confused at some point.我想你在某些时候会感到困惑。 You are calling the function, inside of itself.您在其内部调用 function。 You should do like this, and also, declare the result variable.你应该这样做,并且声明结果变量。

I am sure this will help:我相信这会有所帮助:

    function checkIfDivided(){
// in this section the variables come from an html document
            var number=parseInt(document.getElementById("number").value);
             var divisor=parseInt(document.getElementById("divisor").value);
            if(number%divisor==0)
              return true;
            else return false;
    }


or



    function checkIfDivided(number,divisor){
//in the function the variable are given as parameters
            if(number%divisor==0)
              return true;
            else return false;

    }

Looks like two things to me:在我看来有两件事:

  1. You haven't declared your 'resultado' variable ( this can be as simple as just typing 'let resultado;' without the single quotes你还没有声明你的'resultado'变量(这可以像输入'let resultado;'一样简单,没有单引号

  2. You haven't returned your 'resultado' variable after the if/else statement在 if/else 语句之后,您没有返回“resultado”变量

Right now, your function is using an undeclared variable and not returning anything, so that is why you are getting an error.现在,您的 function 正在使用未声明的变量并且没有返回任何内容,这就是您收到错误的原因。 Fix the two above steps and you should be good: :)修复上述两个步骤,你应该会很好::)

You clearly understand that the modulus operator is the way to go.您清楚地了解模运算符是 go 的方式。 Using it we discover that 12 is divisible by 3 because 12 % 3 return zero.使用它我们发现12可以被3整除,因为12 % 3返回零。 Zero is considered a "falsy" value while any other number is considered "truthy".零被认为是“虚假”值,而任何其他数字都被认为是“真实”。

Given this then if 12 % 3 returns a "falsey" value (zero) we can't use the result directly.鉴于此,如果12 % 3返回“假”值(零),我们不能直接使用结果。 But what if we can "flip" false to true?但是,如果我们可以将假“翻转”为真呢? We can, using the not operator ( ! ).我们可以使用not运算符 ( ! )。

Using the !使用! operator on the result of a math problem requires the use of parentheses around the math problem itself.数学问题结果的运算符需要在数学问题本身周围使用括号。

So the problem in code becomes (12 % 3) and to 'flip' it with the !所以代码中的问题变成(12 % 3)并用! operator it becomes运营商它变成

!(12 % 3) . !(12 % 3)

This is proven below with:这在下面得到证明:

  1. console.log(!(12 % 3)) --> logs true console.log(!(12 % 3)) --> 日志为真
  2. console.log(!(12 % 5)) --> logs false console.log(!(12 % 5)) --> 记录为假

The function implementation of that is simple and also proven: function 的实现很简单,也经过验证:

  1. console.log(isDivisible(12,3)); --> logs true --> 记录为真
  2. console.log(isDivisible(12,5)); --> logs false --> 记录错误

 console.log(.(12 % 3)) console,log(;(12 % 5)) function isDivisible(number. x){ return,(number % x); } console.log(isDivisible(12,3)); console.log(isDivisible(12,5));

There is one other way to do so and i think its much cleaner.还有另一种方法可以做到这一点,我认为它更清洁。

console.log(Number.isInteger(10/2)) //true
console.log(Number.isInteger(4/2)) // false

//a must be greater than b
function check(a,b) {
 console.log(Number.isInteger(a/b))
 return Number.isInteger(a/b)
}

check(10,5)//true
check(8,3)//false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM