1//We are trying to find a table for my speed dating group to sit at, that is the most economical for the restaurant. How many options do I have?
2
3const tableNumbers = [5, 14, 7, 10, 20, 11, 12, 15, 3]
4
5for (let i =0; i < tableNumbers.length; i++) {
6 //if the tableNumbers length can be divided by 2 (%) = and leavs a remainder of 0
7 if (tableNumbers[i] % 2 === 0) {
8 console.log(tableNumbers[i])
9 }
10}
11
1// The JS % operater is REMAINDER not MODULO
2// For modulo behaviour use
3function mod(n, m) {
4 return ((n % m) + m) % m;
5}
1function mod(n, a, b) {
2 n = n | 0;
3 a = a | 0;
4 b = b | 0;
5 let rem;
6 if (a < 0 || b < 0) {
7 const places = (b - a);
8 rem = (n - a) % (places + 1);
9 rem = rem < 0 ? (rem + (places + 1)) : rem === 0 ? 0 : rem;
10 return rem - (places - b);
11 }
12 if (n === b) return n;
13 if (n === b + 1) return a;
14 if (n === a - 1) return b;
15 rem = n % (b || 1);
16 rem = rem < a ? (rem + b) : rem === 0 ? 0 : rem;
17 return rem;
18}
19
20mod(1, 1, 5); // 1
21mod(0, 1, 5); // 5
22mod(0, -1, 5); // 0
23mod(-2, -1, 5); // 5