I have the following variable with some number.
const string = "5,5,5,5";
const string = "5,5,5,5,10";
I pasted it twice to explain what I am trying to do.
I want to add all the numbers and get a total.
The first I did, is cleaning the variable to avoid the commas, when I split it
let stringWithoutSigns = string.replace(/,/g, "");
I paste the full code to better understanding.
const string = "5,5,5,5";
let stringWithoutSigns = string.replace(/,/g, "");
let itemString = stringWithoutSigns.length;
if (itemString > 2) {
result = cleanString();
console.log(result)
}
function cleanString() {
let arrString = stringWithoutSigns.split("").map(function(index) {
return parseInt(index, 10);
});
console.log(arrString);
let sum = arrString.reduce(function(a, b) {
return a + b;
});
return sum;
}
My problem, is that if I type a new number like 10, it doesn't works, because is splitted into 1, 0