Modulo operator

Tell us what’s happening:
I want to know 3.14 modulo 1 is equal to 0 or .14. Someone please explain how modulo works in decimal numbers?

Your code so far


const squareList = (arr) => {
// only change code below this line
let sqrtArray = arr.filter(currNum =>);
return arr;
// only change code above this line
};

// test your code
const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36.

Challenge: Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/use-higher-order-functions-map-filter-or-reduce-to-solve-a-complex-problem

if you do 3.14 % 1 it gives the reminder of the division operation, which is 0.14
3.14 / 1 results in 3 with reminder of 0.14

1 Like

Do you understand what a remainder is in maths? ie the amount left over in integer division.

So 5 ÷ 2. 2 divides evenly into 5 twice. 5 - 2 is 3, there’s one, 3 - 2 is 1, there’s two, can’t subtract any more. The remainder is therefore 1. That operation is a modulo operation. 5 modulo 2 is 1. JavaScript uses the % sign as the operator. 5 % 2 is 1.

Edit: ah snap

1 Like

I wonder 1 x 3.14 equals 3.14 so when we divide 3.14 by 1 how we get 0.14 as remainder. It’s only possible if we divide only integer part ie 3. So quotient will be 3 and remainder will be 0.14. My question is why do we do that ? Since 1x3.14 is 3.14 why can we take 3.14 as quotient ?

the modulus is the reminder after you have put the divisor a whole number of times.
if you do 7 % 2 the reminder is 1, as 7/2 is 3 with reminder of 1 (3 * 2 + 1 == 7)
it is true that 3.5 * 2 == 7 but that’s not the point, we do not want a division with a result that have a fractional part

2 Likes