Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
Why I am getting age[i] as undefined?

Here I am going to describe my problem that why I am getting age[i]=a as undefined when I am going to console it

enter image description here

const birthYear = [1996, 1997, 1998, 1999]

function calc(Year) {
  age = 2022 - Year;
  return age

}
var age = []
age[0] = 123;
console.log(age[0])

for (i = 0; i <= birthYear.length - 1; i++) {
  var a = calc(birthYear[i])

  /* if you want to return the value of any function you have to  address 
     it the help of any agruement otherwise it will show you a function body
   */
  age[i] = a
  // console.log
  console.log(age[i])
}

// console.log(age[])

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The problem is that you return "age" with calc and also called your array "age". Hence the array gets replaced everytime and you're getting "undefined".

Try this :

const birthYear=[1996,1997,1998,1999]

function calc(Year) {
    age=2022-Year;
    return age
}

var ageArr = [];

for(var i = 0 ; i < birthYear.length ; i++){
    ageArr[i] = calc(birthYear[i])
    console.log(ageArr[i]);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

why I am getting age[i] as undefined?

you have used var keyword to declare age array,now the age become global variable,so when you make a call to calC function it converts array to number age=2022-Year; after executing this statement . thats the reason age[i] is undefined . now whats the correct way to do this,just declare ageCount inside calc function and return it,like this

 const birthYear=[1996,1997,1998,1999]
 function calc(Year) 
 {
   //instead of using 2022, you fetch current year from date object
   let ageCount=2022-Year;
   return ageCateCount
}
var age=[]
age[0]=123;
console.log(age[0])

for(i=0;i<=birthYear.length-1;i++){
   age[i]=calc(birthYear[i]);
// console.log

         console.log(age[i])
}

const birthYear = [1996, 1997, 1998, 1999]

function calc(Year) {
  let ageCount = new Date().getFullYear() - Year;
  return ageCount

}
var age = []
age[0] = 123;
console.log(age[0])

for (i = 0; i <= birthYear.length - 1; i++) {
  var a = calc(birthYear[i])

  /* if you want to return the value of any function you have to  address 
     it the help of any agruement otherwise it will show you a function body
   */
  age[i] = a
  // console.log
  console.log(age[i])
}

hope this will help!

about 4 years ago · Juan Pablo Isaza Relatório

0

If I understand the requirement correctly, You want to find the difference from year 2022 to the years in the birthYear array. If Yes, You can simply achieve this with a single line of code instead of making it complex.

Demo :

const birthYear = [1996, 1997, 1998, 1999];

const age = birthYear.map(year => 2022 - year);

console.log(age);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda