Function returning Nan

Asked

Viewed 38 times

-1

Well I am trying to execute this function and results in Nan, not even an error in the console.

var numeros = [10,2,5,,30,25,19,20,50,40,150]

function media(nums) {
    var tam = nums.lenght
    var soma = 0
    for (var i = 0; i < tam; i++) {
        soma += nums[i]
    }

    return Math.round(soma/tam)
}

document.write(media(numeros))

1 answer

0

Two errors: ḍ length' is spelled incorrectly (attributed to the ḍtam'), and a non-numerical value in the vector ¡numeros' (where it contains two commas).

Should stay:

var numeros = [10,2,5,30,25,19,20,50,40,150]

function media(nums) {
  var tam = nums.length
  var soma = 0
  for (var i = 0; i < tam; i++) {
      soma += nums[i]
  }

  return Math.round(soma/tam)
}

document.write(media(numeros))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.