A simple question using % in JS

Asked

Viewed 379 times

1

I’m learning Javascript and came across a question that was solved by another, but used a % (dollar sign) in the code. I’m still too layy to understand, but could someone explain to me the real usefulness of this mathematical symbol?

I think the question would be, how do I interpret this mathematical part when the dollar appears? I would say that the rest is i of 2%? Will be right?

function pares (x, y){
    var i, j;
    i = x;
    j = y;

    for (i = x; i <= j; i++){
        *resto = i % 2;*
        if (resto === 0) {
            console.log('Numero par: ' + Math.floor(i))
        }
    }
}
console.log(pares(32, 321))
  • 3

    That symbol % is dollar?

  • Ta more like a kkk interrogation. Now seriously, this symbol is Alberty percentage, but it has nothing to do with the symbol % used in mathematics.

  • Good, kkk... Being read has its advantages.. Thank you friend

2 answers

5

This is usually called the module operator, but actually it finds the rest of an entire division, people confuse that because it’s very similar (you can see more details of this in the mgibsonbr response). He makes a division but instead of resulting in her value he results in the rest he would give in this division.

In the link above has a formula that approaches how the rest should be calculated in the hand, it is not accurate, it is just to give an idea).

the real usefulness of this mathematical symbol?

Forget the symbol, or treat it as a percentage (even less dollar that would be $). Treat it only as operator rest. In our context this is not a real mathematical symbol, since in mathematics this should result in a percentage calculation. It’s weird, but that’s what you could do. The percentage is easy to get, the rest not so much so they created an operator and used this symbol.

how I interpret this mathematical part when the dollar sign appears?

In the example how you want the rest of a division by 2 there are only two possible results, 0 or 1, therefore:

  • if a number is even the rest is 0
  • if it’s odd it’s 1.

Every integer divisible number will result in 0. This, too, is used to find out which primes or other formulas are.

Would you say that the rest is i of 2%? It will be correct?

You can say: "the rest of i divided by 2"

I made the code more streamlined:

function pares(x, y) {
    for (let i = x; i <= y; i++) if (i % 2 === 0) console.log('Numero par: ' + i);
}
pares(32, 321);

I put in the Github for future reference.

Note that I simplified your code and solved a few things that could potentially cause problems and have more complex code, so pay close attention to it, in all characters.

And know that this case could be more simplified and performative:

function pares(x, y) {
    for (let i = x; i <= y; i += 2) console.log('Numero par: ' + i);
}
pares(32, 321);

I put in the Github for future reference.

  • I appreciate the time, now I understand the meaning and I will copy myself code to study and improve mine, I appreciate the time and all the explanation... Thank you very much.

3


The name of the symbol % is percentage or percentage. It represents the module or mod between two integers.

We use this symbol to know the rest of the division between two values. If it is 0, that is to say, there is nothing left, means that it is divisible, otherwise it is not.

Example:

10 % 2 results in 0, soon, 10 is divisible by 2. (PAR)

5 % 2 results in 1, soon, 5 IS NOT DIVISIBLE BY 2. (ODD)

inserir a descrição da imagem aqui

In the image example would look like this:

12 % 4 that would give 0 (REMNANT)

12 / 4 that would give 3 (QUOTIENT)

That’s why in the function it checks if the rest is equal to zero.

Good studies!

Browser other questions tagged

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