Program to add two integers of given base | Function to find the sum of two integers of base B ; Padding 0 in front of the number to make both numbers equal ; Condition to check if the strings have lengths mis - match ; Loop to find the find the sum of two integers of base B ; Current Place value for the resultant sum ; Update carry ; Find current digit ; Update sum result ; Driver code ; Function Call
function sumBaseB ( a , b , base_var ) { let len_a , len_b ; len_a = a . length ; len_b = b . length ; let sum , s ; s = " " ; sum = " " ; let diff ; diff = Math . abs ( len_a - len_b ) ; for ( let i = 1 ; i <= diff ; i ++ ) s += " " ; if ( len_a < len_b ) a = s + a ; else b = s + b ; let curr , carry = 0 ; for ( let i = Math . max ( len_a , len_b ) - 1 ; i > - 1 ; i -- ) { curr = carry + ( a [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) + ( b [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) ; carry = parseInt ( curr / base_var , 10 ) ; curr = curr % base_var ; sum = String . fromCharCode ( curr + ' ' . charCodeAt ( ) ) + sum ; } if ( carry > 0 ) sum = String . fromCharCode ( carry + ' ' . charCodeAt ( ) ) + sum ; return sum ; } let a , b , sum ; let base_var ; a = " " ; b = " " ; base_var = 6 ; sum = sumBaseB ( a , b , base_var ) ; document . write ( sum + " " ) ;