Wednesday, May 5, 2021

Javascript Practice 30: JS Hero - Modulo

 
Question: 

Write a function onesDigit that takes a natural number and returns the ones digit of that number.

Example: onesDigit(2674) should return 4.




Answer: 

function onesDigit (number) {

return number % 10; 

}; 

> modulo / modulus operation returns the remainder of a division, after one number is divided by another (called the modulus of the operation)
> for eg. 25 mod 10 (or 25 % 10) would evaluate to 5, because 25 divided by 10 has a quotient of 2 and a remainder of 5
> this arithmetic operation will always return the ones digit of any number (parameter) we provide
> notice that doing division with a calculator will not show the result of the modulo operation, and that the quotient will be expressed as a decimal fraction if a non-zero remainder is involved. 

No comments:

Post a Comment