Function wont process more than the first argument?

Unsure why this function isnt working
It only does one item then stops

Link to problem


function predictAge(age)
{
  var sumAgesSquared;
  var sqrRootDividedbyTwo= Math.sqrt(sumAgesSquared)/2
  for(var i = 0 ; i<age.length ; i++) 
  {
    sumAgesSquared+=age[i]**2;
  }


  return sqrRootDividedbyTwo
  
}

You are failing some Javascript basics. Output to console.log() would reveal these. Your coding environment supports console api
https://www.w3schools.com/js/js_debugging.asp

You have not successfully initialized either of your two local variables. sumAgesSquared was simply not given an initial value. Since sqrRootDividedbyTwo is initalized as sqrRootDividedbyTwo= Math.sqrt(sumAgesSquared)/2 that also fails.

age is not an array. You are sending multiple parameters to your function but only capturing the first one in a variable age. If unsure of the number of parameters you’ll have to access all of these using the arguments object.

Consequently, age.length is undefined because of above.

I would suggest starting with multiple parameters issue and then tackle the uninitialized local variables afterwards. Use the console to verify the results of your changes.

Good luck.

1 Like