Suppose we have a string containing only digits:
let test = '12345'; // string with digits
Let's find, for example, the sum of its first and second characters:
let test = '12345';
alert(test[0] + test[1]); // shows '12' - concatenates like strings
As you can see, the characters in our string
are also strings and are concatenated as strings.
Let's say we want to sum them as numbers.
We use the function Number
for this aim:
let test = '12345'; // string
alert(Number(test[0]) + Number(test[1])); // shows 3
Given the string '12345'
.
Find the sum of the digits of this string.