Help with string into array and sum please

Hi all, I’m really struggling to see what I’m doing wrong if someone could help please. I feel like this is quite basic but I’m super stuck!
I need to take a string (eventually this could include letters) and total it eg so ‘1,2 3’ = 6. I can split the string into an array but I can’t seem to then correctly apply a loop to create the sum total for me.
If I change it to an array manually the for loop works so I ‘think’ there is something going on between me splitting and the loop. thank you!

   const string = '4,2,3,4,5,6,7,1,2,4';
   let myString = string.split('');
   let totalScore = 0;
   for (i = 0; i < myString.length; i++) {
      totalScore += myString[i];
//output 04,2,3,4,5,6,7,1,2,4

I am also just learning, but this is how I understand it.
If you try to display myString on the console, the problem becomes clear:


//output [
  '4', ',', '2', ',', '3',
  ',', '4', ',', '5', ',',
  '6', ',', '7', ',', '1',
  ',', '2', ',', '4'

As it was originally a string, it is still a string when split. Also there are commas left out in the array apart from the numbers. So instead of an addition operation, you get string concatenation and instead of 0 + 4 = 4 you get 0 + 4 = 04


ah yes thank you so much!

1 Like