For non-negative integers num1
and num2
, represented as strings, return the sum of num1
and num2
.
My solution was to create a filter that has the indexOf with a value of the numbers.
const addStrings = function(num1, num2) {
let filter = "0123456789";
let x = 0;
for(let i=0; i<num1.length; i++) {
x = x + (filter.indexOf(num1[i]) * Math.pow(10, num1.length-1-i));
}
let y = 0;
for(i=0; i<num2.length; i++) {
y = y + (filter.indexOf(num2[i]) * Math.pow(10, num2.length-1-i));
}
return (x+y).toString();
};
It work in most of the cases. However, if the inputs are:
"9333852702227987"
"85731737104263"
It will return the wrong sum: "9419584439332252". I can't understand why it is converting the numbers wrongly.