Error passing array as function parameter. Uncaught Typeerror: cards is Undefined

Asked

Viewed 42 times

-1

Guys, I’m taking a course to learn Javascript and I’ve been in this problem for a long time and I can’t understand what happens.

I need to create a function that allows me to count how many times one of the car plates appears in this array. I can perform normally when I do not pass the parameter to the function, but when I put the parameter and call the function I get the error "Uncaught Typeerror: placas is Undefined", but the variable plate is set yes, maybe it’s something simple that I missed, I do not know.

Without passing the cards array as parameter: Sem passar o array placas como parâmetro:

<script>

var placas = [
   'RXB-2525', 'AKX-3333', 'ORO-7142','RXB-2525', 'AKX-3333', 'ORO-7142',
   'AKX-3333', 'RXB-2525', 'AKX-3333','AKX-3333', 'RXB-2525', 'AKX-3333','RXB-2525', 'AKX-3333', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
   'AKX-3333', 'ORO-7142', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
   'AKX-3333', 'AKX-3333', 'RXB-2525','AKX-3333', 'AKX-3333', 'RXB-2525',
   'AKX-3333', 'ORO-7142', 'ORO-7142','AKX-3333', 'ORO-7142', 'ORO-7142',
   'ORO-7142', 'RXB-2525', 'AKX-3333','AKX-3333', 'ORO-7142', 'ORO-7142',
   'AKX-3333', 'RXB-2525', 'AKX-3333','AKX-3333', 'RXB-2525', 'AKX-3333',
   'RXB-2525', 'AKX-3333', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
   'AKX-3333', 'ORO-7142', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
   'AKX-3333', 'AKX-3333', 'RXB-2525','AKX-3333', 'AKX-3333', 'RXB-2525'
]


function calcularNumeroDeEntradas() {
  
   var cont = 0

 for(var i = 0; i<=placas.length;i++) { 
  
      if (placas[1] == [i]) {

         cont++; 
      }

}

return cont; 
}

When I pass the array as a function parameter: inserir a descrição da imagem aqui

<script>

var placas = [
   'RXB-2525', 'AKX-3333', 'ORO-7142','RXB-2525', 'AKX-3333', 'ORO-7142',
   'AKX-3333', 'RXB-2525', 'AKX-3333','AKX-3333', 'RXB-2525', 'AKX-3333','RXB-2525', 'AKX-3333', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
   'AKX-3333', 'ORO-7142', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
   'AKX-3333', 'AKX-3333', 'RXB-2525','AKX-3333', 'AKX-3333', 'RXB-2525',
   'AKX-3333', 'ORO-7142', 'ORO-7142','AKX-3333', 'ORO-7142', 'ORO-7142',
   'ORO-7142', 'RXB-2525', 'AKX-3333','AKX-3333', 'ORO-7142', 'ORO-7142',
   'AKX-3333', 'RXB-2525', 'AKX-3333','AKX-3333', 'RXB-2525', 'AKX-3333',
   'RXB-2525', 'AKX-3333', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
   'AKX-3333', 'ORO-7142', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
   'AKX-3333', 'AKX-3333', 'RXB-2525','AKX-3333', 'AKX-3333', 'RXB-2525'
]


function calcularNumeroDeEntradas(placas) {
  
   var cont = 0

 for(var i = 0; i<=placas.length;i++) { 
  
      if (placas[1] == [i]) {

         cont++; 
      }

}

return cont; 
}
 

  • function calcularNumeroDeEntradas(placas) {} indicates that you must pass a parameter in order to be able to access the function, that is, when calling it, you must invoke it in the following way: var placas = ['...', '...']; calcularNumeroDeEntradas(placas);. I recommend reading https://answall.com/a/383294/99718

1 answer

0

Call your function and pass boards as argument: calcularNumeroDeEntradas(placas)

Javascript scope at the beginning is confusing. Global scope, block function. But over time this becomes clear and when to use.

source: Function statement

var placas = [
    'RXB-2525', 'AKX-3333', 'ORO-7142','RXB-2525', 'AKX-3333', 'ORO-7142',
    'AKX-3333', 'RXB-2525', 'AKX-3333','AKX-3333', 'RXB-2525','AKX-3333',
    'RXB-2525','AKX-3333', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'ORO-7142', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'AKX-3333', 'RXB-2525','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'ORO-7142', 'ORO-7142','AKX-3333', 'ORO-7142', 'ORO-7142',
    'ORO-7142', 'RXB-2525', 'AKX-3333','AKX-3333', 'ORO-7142', 'ORO-7142',
    'AKX-3333', 'RXB-2525', 'AKX-3333','AKX-3333', 'RXB-2525', 'AKX-3333',
    'RXB-2525', 'AKX-3333', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'ORO-7142', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'AKX-3333', 'RXB-2525','AKX-3333', 'AKX-3333', 'RXB-2525'
 ]

function calcularNumeroDeEntradas(placas) {
  let cont = 0;
  const plate = placas[1]
  for (var i = 0; i <= placas.length; i++) {
    if (plate == placas[i]) {
      cont++;
    }
  }

  return {plate, cont}
}

console.log(calcularNumeroDeEntradas(placas))

Browser other questions tagged

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