Wednesday, May 5, 2021

Javascript Practice 35: JS Hero - Rounding

 
Question: 

Write a function round100 that rounds a number to the nearest hundred.

Example: round100(1749) should return 1700 and round100(856.12) should return 900.




Answer: 

function round100 (number) { 

return Math.round (number / 100) * 100; 

}; 

> this was a bit tough for me
> i spent almost an hour understanding the logic behind this
> however I did a lot of googling to understand this
> its fairly simple in excel, i checked the function Round in excel
> i also realized that I can always check and experiment on the console to get some hints
> however you are supposed to be good not only in math but mainly in logic to execute this in JS
> this question needed me to round off any number to the nearest 100 
> lets say the number is 149
> lets break the above equation Math.round (number / 100) * 100
number / 100: 149 / 100 = 1.49
Math.round(1.49) =  1 (check this in the debugging console)
Math.round(1.49)*100 =  100
> this maybe easy for a lot of you but I was and am still very weak with Maths & Logic

No comments:

Post a Comment