Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
CD - SLR - RegEx, Match, Length, Null Catch solution in Clear category for Count Digits by Taylor_Page
import assert from "assert";
//const countDigits = (text: string): number => text.match(/\d/g)?.length || 0
function countDigits(text: string): number {
return text.match(/\d/g)?.length || 0
}
console.log('Example:');
console.log(countDigits('hi'));
// These "asserts" are used for self-checking
assert.equal(countDigits('hi'), 0);
assert.equal(countDigits('who is 1st here'), 1);
assert.equal(countDigits('my numbers is 2'), 1);
assert.equal(countDigits('This picture is an oil on canvas '
+ 'painting by Danish artist Anna '
+ 'Petersen between 1845 and 1910 year'), 8);
assert.equal(countDigits('5 plus 6 is'), 2);
assert.equal(countDigits(''), 0);
console.log("Coding complete? Click 'Check' to earn cool rewards!");
May 19, 2020
Comments: