This question already has answers here:
What does % do in JavaScript?
(10 answers)
Closed 7 years ago.
When I was repairing a Project via Java that I did about a year ago I came across a piece of Code using n%7 and its a mystery to me what it does. What I understand what it exactly means - Not referring to %.
Thanks in advance,
DR
It is called modulus. It can be read as n modulus 7.
The MODULUS Operator %
The modulus operator finds the modulus of its first operand with respect to the second. That is, it produces the remainder of dividing the first value by the second value. For example:
22 % 6 = 4 because 22 / 6 = 3 with a remainder of 4
read more here: http://mathbits.com/MathBits/Java/DataBasics/Mathoperators.htm
Related
This question already has an answer here:
Javascript integer beginning with zero [duplicate]
(1 answer)
Closed 5 years ago.
I have created a calculator but it has such problem
var x=060;
var y=60;
console.log(x+y);
The output is 108 why so?
And, How can i make calculator to enter single operator and wait for an operand and then an operator could appear in.
This is my calculator
Calculator2.0
Put 0 in front of a number means the base is octal. In your case 060 (octal) = 48 (decimal). You add 60 + 48 that is equal to 108.
This question already has answers here:
Why JavaScript treats a number as octal if it has a leading zero
(3 answers)
Closed 5 years ago.
We're having a discussion in the office about how the hell this math works in JavaScript.
There was an instance where we were multiplying by 010 instead of 10 and this gave the incorrect returned value.
For example...
25.25 * 010 = 202
25.25 * 10 = 252.5 as expected
whats even weirder is if you do parseFloat(010) it gives you 8!
For 010 is decimal 8, so it get 202.
console.log(25.25 * 010);
Look at this answer for Java: Why "010" equals 8?. JavaScript has to do the same.
The answer is, that an octal number starts with a leading zero 0.
This question already has answers here:
What's the difference between & and && in JavaScript?
(4 answers)
What does this symbol mean in JavaScript?
(1 answer)
Closed 6 years ago.
I am looking through the source of a library I'm working with, and I found something I haven't seen before.
$(item).each(function(child) {
oddEven = (i & 1);
targetNode.append(jasper_build_product(this,oddEven));
i++;
});
Notice the oddEven = (i & 1);. What does the (i & 1) part do? I'm particularly curious about the ampersand.
The & operator is a bitwise AND, and more specifically the expression x & 1 returns the least significant bit (LSB) of the value x.
Since the internal representation of numbers is base-2, an LSB of 1 indicates an odd value, and 0 indicates an even value.
This question already has an answer here:
Why does alert("1" - - "1"); produce 2 in javascript?
(1 answer)
Closed 8 years ago.
Why does alert("1" - - "1") return 2?
I'm not entirely sure what is going on here to create the result as 2?
I came across the problem here:
http://davidshariff.com/js-quiz
it's like writing 1 - (-1) which is 1 + 1 = 2
the problem in js is that if you use + between 2 string it means concating them thus resulting 11
EDIT: thanks to iamnotmaynard comment i was able to find this post
Why does JavaScript handle the plus and minus operators between strings and numbers differently?
This question already has answers here:
What does % do in JavaScript?
(10 answers)
Closed 9 years ago.
How can I use modulo operator (%) in calculation of numbers for JavaScript projects?
It's the remainder operator and is used to get the remainder after integer division. Lots of languages have it. For example:
10 % 3 // = 1 ; because 3 * 3 gets you 9, and 10 - 9 is 1.
Apparently it is not the same as the modulo operator entirely.
That would be the modulo operator, which produces the remainder of the division of two numbers.