Share your repls and programming experiences

← Back to all posts
function and loop help needed
OzzieHarb (0)

The function isn't adding up the numbers it's called with

Commentshotnewtop
DynamicSquid (2673)
function totalGoals(num) {
  return num // this is the mistake

  let goalsScored = [num];
  let totalGoals = 0;

  for (let num = 0; num < goalsScored.length; num++) {
    totalGoals = totalGoals + goalsScored[num];
  }

  return totalGoals
}

not sure what you're doing on line 3... a return statement exits the function completely

OzzieHarb (0)

@DynamicSquid thanks, I took out line 3 but it's still not adding up the numbers it gets called with. Is it line 6? totalGoals = totalGoals + goalsScored[num]

DynamicSquid (2673)

@OzzieHarb ok, so you're passing an array through the function, totalGoals([0,1,2]), but then you create an array of that array: let goalsScored = [num]; Like an array inside an array. I think you meant to do goalsScored = nums. Also, the parameter name in num, and the for loop variable is named num. Might wanna change that up to avoid any confusion.