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

121
Visualizações
I get undefined at start of array in js

I made this code :

function get_coordinates(container) {
    var x;
    var y;
    var divs = container.getElementsByTagName('div');

    Array.from(divs).forEach(div => {
        y += div.offsetTop+" ";
        x += div.offsetLeft + " ";
    });
    const a = x + ";" + y;
    console.log(divs);
    return x+";"+y;
}


this is the console log :

HTMLCollection(10) [div#selector_1.selector, div#selector_2.selector, div#selector_3.selector, div#selector_4.selector, div#selector_5.selector, div#selector_6.selector, div#selector_7.selector, div#selector_8.selector, div#selector_9.selector, div#selector_10.selector, selector_1: div#selector_1.selector, selector_2: div#selector_2.selector, selector_3: div#selector_3.selector, selector_4: div#selector_4.selector, selector_5: div#selector_5.selector, …]

and this the return :

undefined0 0 0 0 0 0 0 0 0 0 ;undefined0 0 0 0 0 0 0 0 0 0

I dont understand why i get undefined at the start of both.

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

0

As Musa advised in an earlier comment, you do not initialize x and y, so basically what you are trying to do is the following:

y += div.offsetTop + " ";
x += div.offsetLeft + " ";

That translates to

undefined += div.offsetTop + " ";
undefined += div.offsetLeft + " ";

At the top you should define that x and y are strings, for example:

let x = "";
let y = "";

Also, I would suggest using let and const instead of var, it’s 2022, so no need for var anymore.

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