Back to questions
JuniorTheoryCommon
What does the program print?
console.log(7 / 2, -7 % 3, Math.floor(7 / 2));3.5 -1 3
- Expecting
3for7 / 2: JavaScript has onenumbertype for whole numbers and fractions, so/keeps the fraction and gives3.5. - Expecting a non-negative remainder, as in math: in JavaScript the remainder takes the sign of the number being divided, so
-7 % 3is-1.
- What does
Math.trunc(-7 / 2)print, and how does it differ fromMath.floor(-7 / 2)? - How would you get a remainder that is never negative for
-7and3?