-1

I created a function with a regular expression and then iterated over the array by adding the previous total to the next index in the array.

My code isn't working. Is my logic off? Ignore the syntax

function sumofArr(arr) { // here i create a function that has one argument called arr
  var total = 0; // I initialize a variable and set it equal to 0 
  var str = "12sf0as9d" // this is the string where I want to add only integers
  var patrn = \\D; // this is the regular expression that removes the letters
  var tot = str.split(patrn) // here i add split the string and store it into an array with my pattern
  arr.forEach(function(tot) { // I use a forEach loop to iterate over the array 
    total += tot; // add the previous total to the new total
  }
  return total; // return the total once finished
}
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
Phoenox7
  • 1
  • 1
  • 1
  • 1
    \\D is not a valid regular expression. And you are adding strings not numbers – epascarello Aug 30 '16 at 20:43
  • Have you tried stepping through it to see where it breaks? – Wayne Aug 30 '16 at 20:44
  • 1
    Have you [checked your console for errors?](http://stackoverflow.com/documentation/javascript/185/hello-world/714/using-console-log) You say to ignore the syntax but if the syntax is wrong, the program won't run at all. – Mike Cluck Aug 30 '16 at 20:44
  • 1
    Indent your code ! and `var patrn` should be : `var patrn = "\\D"` – Gilles Quénot Aug 30 '16 at 20:45
  • 1
    When you say "isn't working", what is actually happening? Are you getting errors, or output that doesn't match expected? – StephenTG Aug 30 '16 at 20:45

3 Answers3

4
var patrn = \\D; // this is the regular expression that removes the letters

This is not a valid regular expression in JavaScript.

You are also missing a closing bracket in the end of your code.


A simpler solution would be to find all integers in the string, to convert them into numbers (e.g. using the + operator) and summing them up (e.g. using a reduce operation).

var str = "12sf0as9d";
var pattern = /\d+/g;
var total = str.match(pattern).reduce(function(prev, num) {
  return prev + +num;
}, 0);

console.log(str.match(pattern)); // ["12", "0", "9"]
console.log(total);              // 21
TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
1

you have some errors :

change var patrn = \\D with var patrn = "\\D"

use parseInt : total += parseInt(tot);

function sumofArr(arr){ // here i create a function that has one argument called arr
var total = 0; // I initialize a variable and set it equal to 0 
var str = "12sf0as9d" // this is the string where I want to add only integers
var patrn = "\\D"; // this is the regular expression that removes the letters
var tot = str.split(patrn) // here i add split the string and store it into an array with my pattern

arr.forEach(function(tot){ // I use a forEach loop to iterate over the array 
total += parseInt(tot); // add the previous total to the new total
})
return total; // return the total once finished
}

alert(sumofArr(["1", "2", "3"]));

https://jsfiddle.net/efrow9zs/

Mustapha Larhrouch
  • 3,373
  • 3
  • 14
  • 28
  • So the alert returns 6, but the goal was to return the str which would result to 21. Can you please explain the reasoning of your alert? – Phoenox7 Sep 01 '16 at 05:33
0
function sumofArr(str) {
 var tot = str.replace(/\D/g,'').split('');
   return  tot.reduce(function(prev, next) {
   return parseInt(prev, 10) + parseInt(next, 10);
});}

sumofArr("12sf0as9d");

jiveturkey
  • 31
  • 5