Skip to content

Instantly share code, notes, and snippets.

Created August 1, 2017 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/b0f73611ce9506fc330fd66c171d5e0d to your computer and use it in GitHub Desktop.
Save anonymous/b0f73611ce9506fc330fd66c171d5e0d to your computer and use it in GitHub Desktop.
Long String Number Add return as decimal not as scientific notation
function sumStrings(a,b) {
/*
What this does is a brute force addition of numbers as strings
Purpose: to avoid the conversion to scientific notation and display
in Decimal notation as a string.
Input: a, b numbers in string format
Return: total of addition of a and b in string format.
*/
let x = [];
let y = [];
let total = []; //initialise the string.
let subtotal = 0;
// lots of duplicate code from here. Could probably be added to a helper function.
for (let adigit of a){
x.push(adigit);
}
for (let bdigit of b){
y.push(bdigit);
}
let tempArr = x.slice();
for(let checkLeading of x){
if (checkLeading == 0){
tempArr.shift();
} else{
// break on first occurance of
break;
}
}
x = tempArr.slice();
tempArr = y.slice();
for(let checkLeading of y){
if (checkLeading == 0){
tempArr.shift();
} else{
// break on first occurance of
break;
}
}
y = tempArr.slice();
x = x.reverse();
y = y.reverse();
// all this code up to here is duplicated. This should probably be added to a helper function
let carry = 0;
//decide which is the longest number for the maximum loop length
let max_len = x.length > y.length ? x.length : y.length;
for (let position =0; position < max_len; position++){
//might get an element from the array that does not exist
// and you don't know if a or b is the largest number
//test for this condition
if (x[position] == undefined){
subtotal = parseInt(y[position],10) + 0 + parseInt(carry,10);
carry = 0; // reset the carry
}else if (y[position] == undefined){
subtotal = parseInt(x[position],10) + 0 + parseInt(carry,10);
carry = 0; // reset the carry
}else{
//if neither is undefined, add them with any carry.
subtotal = parseInt(x[position],10) + parseInt(y[position],10)+ parseInt(carry,10) ;
carry = 0; // reset the carry
}
if (subtotal > 9){
carry = 1; //don't forget to include the carry.
//remove the carry from the subtotal.
subtotal -=10;
}
//add it to the front of the array so the number is in the correct order
total.unshift(subtotal);
}
//there may be a case where a carry occurs with the last number and the loop is complete
//add the carry here
if (carry == 1){
total.unshift(carry);
}
let decimalSum = "";
for (let strNumber of total){
decimalSum = decimalSum + String(strNumber);
}
return(decimalSum);
}
console.log(sumStrings('00103', '08567'));
console.log(sumStrings('523','556'))
console.log(sumStrings('123','456'))
console.log(sumStrings('712569312664357328695151392', '8100824045303269669937') )
console.log(sumStrings('712569312664357328695151392', '810082404530326966993712121212121') )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment