Skip to content

Instantly share code, notes, and snippets.

@ginpachiSensei
Created January 20, 2023 07:19
Show Gist options
  • Save ginpachiSensei/394e7d70854cbc689d133abec5aaeb84 to your computer and use it in GitHub Desktop.
Save ginpachiSensei/394e7d70854cbc689d133abec5aaeb84 to your computer and use it in GitHub Desktop.
let value = "12AB34";//given string
let val1 = value.split("");//split string
//filter out the character and keep numbers
let val2 = val1.filter(c =>{
if (c >= '0' && c <= '9') {
return c;
}
});
//sum all the numbers in array
let sum = 0;
val2.forEach(x => {sum += parseInt(x);});
console.log(sum);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment