Sum of characters in a string?

Sum of characters in a string?
0

#1

Confused on this one and not sure if I need to use parseInt or what. But I have an array right now and am iterating through, but I need to add them together?

Instructions:

Write a function named sumLetters that returns the sum of every character in the string

Example:

If you pass "45" it should return 9
If you pass "246" it should return 12

My code:

function sumLetters(str) {
    let sum = 0;
    let splitStr = str.split('');
    
    for (let i = 0; i < splitStr.length; i++) {
        
    }
    
    return sum;
};

#2

It is strange that the function name is sumLetters. Do some of the test cases have letters in the str passed into the function (i.e. “4K8”)?


#3

No, just a horrible function name haha


#4

That means all the test cases have numbers as arguments right?


#5

You could use parseInt if you want, because each character in the splitsStr array will be strings and you can not add strings to get a number.


#6

Is there a more logical way to go about this?


#7

Your logic looks fine so far, but you you will need to convert each character to a number before adding to the sum variable. You can use parseInt or try to coerce the string into a number a different way.

FYI - Technically, you do not need to convert str into an array. You can just iterate through each letter of the string (via an index) and accomplish the same thing.


#8
function sumLetters(str) {
    let sum = 0;
    let splitStr = str.split('');
    
    for (let i = 0; i < splitStr.length; i++) {
        sum += parseInt(splitStr[i])
    }
    
    return sum;
};

Seems to work?

EDIT: Now, I’m trying not to split the string and just work from there…I have this currently:

function sumLetters(str) {
    let sum = 0;
    for (let i = 0; i < str.length; i++) {
        sum += str[i];
    }
    
    return sum;
};

#9

Seems to.

You could also try:

const sumLetters = str => {
  let sum = 0;
  for (let char of str) {
    sum += +char;
  }
  return sum
};

or if you want to use an array:

const sumLetters = str => [...str].reduce((sum, char) => sum + +char, 0);

#10

Hard to keep up with that second one. I’m hardly familiar with for…of loops?


#11

If you think about strings as an array of characters, then the for… (of) loop simply iterates over every character in that string.

In the second example, again treating str as an array of characters, use the spread operator to actually make it an array, then use the Array.reduce() function to sum each character. It is a fairly advanced way of looking at functions, using the “fat arrow function” and its shorthand, but when you get comfortable with that approach, it make sense. Rather than seeing how you should iterate over a collection of things (“should I use a for loop?”, “should I use a while loop?”), you simply see what you want to do with the data itself.

Methods like reduce, map, and filter are great in that they abstract the how and leave you to handle the what.