0
What’s the logic
What is the output of this code? var arr = [2, 3, 5, 8]; var str='ll. arr.forEach(x=>{ str+=x % 3; }): console.log (str); Output: 2022 What’s the logic behind it? I’m doing 2%3 3%3 5%3 8%3
1 Antwort
+ 3
G'day again. Have you learned the difference between division and modulo?
Division gives you the quotent ignoring any remainder , eg 7/3=2 ignores the 1 remainder.
Modulo gives the remainder ignoring the quotient, eg 7%3=1 remainder, ignores the 2 lots of 3.
Your code seems to give you remainder of 2%3 is 2, 3%3 is 0, 5%3 is 2, 8%3 is 2.
It concatenates (appends,adds,sticks on the end) each result digit to your string, which is like adding t + t + n + t gives ttnt.