如何找到可被 3 整除的字符串中存在的所有整数的总和?

how to find the sum of all integers present in a string which is divisible by 3?

找到一个字符串中所有数字的总和,它可以被 3 整除,并找到最后一个这样的数字(使用 JavaScript)。示例“8 个中最好的 6 个将获得 9 分”,sum = 15,last = 9。

当然 - 使用 splitreducefilter%(取模)进行整除:

const str = "The best 6 of 8 will get 9 points";

const strArr = str.split("");
const threesArr = strArr.filter(e => parseInt(e) % 3 == 0);
const sumOfThrees = threesArr.reduce((acc, curr) => acc + parseInt(curr), 0);

const allNumbers = strArr.filter(e => parseInt(e));
const lastNumber = allNumbers[allNumbers.length - 1];

console.log("Sum: " + sumOfThrees);
console.log("Last: " + lastNumber);