My code works for some input but for an input like this: ('This'+ 'painting'+ '1845 and 1910') I get an error: AssertionError 7 == 8. I can solve it using regular expression but I don't know what I'm doing wrong in this. Thanks for answering.
import assert from "assert";
function countDigits(text){
let num = text.split('');
let sum = 0;
for (let i=0; i<num.length; i++){
if (Number(num[i])) {
sum += 1;}}
return sum
}
assert.equal(countDigits('This'
+ 'painting'
+ '1845 and 1910'), 8);