Here is code for summing up all digits of number in a String.
Code:
    var delimitedString="9 nine 27 twenty seven 36 thirty six 45 forty five 54 
      fifty four 63 sixty three 72 seventy two 81 eighty one 90 ninty"
    var pattern = /[\d{1,}]/g
    var result = delimitedString.match(pattern)
    console.log("Match Result:",result)
    var initialValue = 0;
    var sum = result.reduce(
        (accumulator, currentValue) => accumulator + parseInt(currentValue), 
      initialValue
    );
    console.log("Sum Is:",sum);
Output:
    Match Result: [
        '9', '2', '7', '3', '6',
        '4', '5', '5', '4', '6',
        '3', '7', '2', '8', '1',
        '9', '0'
    ]
    Sum Is: 81
No comments:
Post a Comment