Today i had an Interview with one of the company and interviewer guy (Ritesh) asked me very simple problem that “How will you add two numbers if numbers are very large/Add large number in JavaScript?”. Frankly saying, i never gave a thought about this problem ever. My interviewer didn’t got got impressed but still i thought of solving it. So below id the function that i have coded for it. Problem: Javascript support at most 53 bit of integer only. If number goes beyond it, Javascript loose precision. Small number can be added simply as follow:
1 2 3 4 5 6 7 8 |
function addNumber(n1,n2){ return n1+n2 } console.log(addNumber(123,123)) OUTPUT: 246 |
But if you try …