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

javascript - Check if number is divisible by another number in JS - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Feb 21, 2021 at 18:09 atultw 1,0199 silver badges16 bronze badges asked Feb 21, 2021 at 17:26 Mauro MartinsMauro Martins 511 silver badge4 bronze badges 4
  • What is the error? please share the error – Or Assayag Commented Feb 21, 2021 at 17:30
  • What's the error if I may ask? – itachi Commented Feb 21, 2021 at 17:30
  • You are calling a function within itself - this is called recursion. I don't understand why you need to do this. Using the % operator as you have done should to tell you whether the number is divisible by the other number. Then get your function to return a result - return ... – A Haworth Commented Feb 21, 2021 at 17:33
  • All you have to do inside your function is return !(numero % x); It is that simple, nothing else is required. The reason this works is because 0 is a "falsy" value while any other number is a "truthy" value. – Randy Casburn Commented Feb 21, 2021 at 17:34
Add a ment  | 

7 Answers 7

Reset to default 3

So you want to check if a number numero is divisible by 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. You should do like this, and also, declare the result variable.

I am sure this will help:

    function checkIfDivided(){
// in this section the variables e 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

  2. You haven't returned your 'resultado' variable after the if/else statement

Right now, your function is using an undeclared variable and not returning anything, so that is why you are getting an error. Fix the two above steps and you should be good! :)

You clearly understand that the modulus operator is the way to go. Using it we discover that 12 is divisible by 3 because 12 % 3 return zero. 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. But what if we can "flip" false to true? We can, using the not operator (!).

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 bees (12 % 3) and to 'flip' it with the ! operator it bees

!(12 % 3).

This is proven below with:

  1. console.log(!(12 % 3)) --> logs true
  2. console.log(!(12 % 5)) --> logs false

The function implementation of that is simple and also proven:

  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

The error came because of the recursion ing from line solucao(numero % x);. Since you already used the variable name "solucao", you could try assigning the modulus operation result to a different variable, for example: const solution = numero % x. Next, we should declare variable resultado that could look like this:

let resultado;
// continue with your code
if (solution === 0) {
    resultado = true;
} else {
    resultado = false;
}

After that you also need to return the value of resultado, for example like that: return resultado;

Your full code could look like that:

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

    return resultado;
}

Since the operation of modulus if a number is divisible returns 0, you could also return a shorter version that only checks if the result equals 0:

function solucao(numero, x) {
    return numero % x === 0
}
发布评论

评论列表(0)

  1. 暂无评论