Function to check if one number is divisible by another

Asked

Viewed 201 times

-3

The objective of this exercise is to return if the input number is divisible by x or not.

Your program will always receive input: an integer number and an integer number representing x and should print on the screen if the first entry number is divisible by x.

The input will always be two integer numbers.

The output should always be true or false.

Sample Input 0

10 2

Sample Output 0

true

Explanation 0

10 is divided by 2 because by dividing 10 by 2 the rest is 0.

That’s the code I’m using:

function solucao(numero, x) {
// seu código aqui
    return numero % x == 0; {
        console.log(solucao (10,2))
        if (resto == 0) {
          console.log("true");
        } else {
            console.log("false");
        }
    } 
}
  • 1

    and what is the difficulty? put in question the code you have done so far and where you have difficulty

  • So, I’m lost in the question, how would I code in and such

  • 1

    if you don’t know where to start best to go back to the basics. Here in the OS we do not give this help, we can help you with a specific problem but we will not do the code for you, because then you will not learn :) search on the site that will probably find question about all the basics of javascript, how to read information, validate, etc

  • the code I was trying to

  • Function solucao(numero, x) { const resto = numero % 2; if (resto == 0) { console.log("true") } } solved(10);

  • but it’s wrong, and I’m half lost! Trying to learn only, but here’s the thing

  • now ta best Leonidas, click here to edit your question and add that code there

  • I edited, could you give me some hint?

  • 1

    You’re seeing if the number is divisible by 2, not by x, then it should be function solucao(numero, x) { return numero % x == 0; } and to call, console.log(solucao(10, 2)) (to see if 10 is divided by 2), console.log(solucao(90, 3)) (to see if 90 is divisible by 3), etc - oh yes, I also made the function return true or false, instead of printing the value (and you only print if it’s true, but what if it’s not? ) - because then whoever called the function only takes the result (true or false) and decides what to do with it (can even print a message, if applicable)

  • Stackoverflow increases the mutual help of software developers. Your question doesn’t seem useful to me, or to most of the Stackoverflow community. Even if I gave you the answer, you wouldn’t understand why I’m telling you. You have the function poorly implemented and poorly called... I suggest you take a basic course at Khan Academy, for example, or even see some tutorials on Youtube. Don’t give up, it’s about wanting to learn!

Show 5 more comments

2 answers

1

The equality operator == checks if your two operands are equal, returning a result boolean, true or false. So no need to test the remnant to print strings with the results.

When a statement return is used in a function body, the execution of that function is stopped and the code after the declaration will not be interpreted. If specified, a given value is returned to the one who called the function. If the expression is omitted, undefined will be returned.

//n= é o numero atestar a divisibilidade.
//d= é o divisor.
function solucao(n, d) {
  return n % d == 0;
}

console.log(solucao(10, 2))

console.log(solucao(7, 3))

0

I will post here some comments on your code:

// aqui não está usando "x" e também não está passando esse valor , então pode remover
function solucao(numero) {   // <- sem o "x"
    const resto = numero % 2; 
    if (resto == 0) { 
       console.log("true") 
    } else {
       console.log("false")  // <- faltou isso, que é o "else" caso o resto não seja zero
    }
};


solucao(10);
solucao(7);

  • I don’t know what but say wrong! In the question it says lower that "The input will always be two integers" and "The output should always be true or false."

  • Me using this code: Function solucao(numero, x) { // your code here Return number % x == 0; { console.log(fix (10,2)) if (rest == 0) { console.log("true"); } } }

  • is wrong based on your question, is waiting for the variable "x" that is not used. If you need to correct edit the question, but I think this should help solve it :)

  • I did the editing I posed the whole question and how I’m using the code now

  • Is that solved then? If you have another different question you can open another question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.