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

207
Visualizações
there's an error I can't detect in my program

Okay guys, this is a follow up to a previous question I asked, about the arrays. It turns out your answers were quite helpful, but my program still won't work. I'll explain the assignment and then post the code, so you can see what the problem is. So I have an html page with an unsorted list of cars. Each car has a a key, a manufacturer and a model. There's a form for inserting a new car on the bottom of the page and two buttons. By clicking one button you need to create an object, which represents a car, with the data that was inserted. That object needs to be added to a global array. There's another button, that has the task to insert the object from the middle of the global array to the html list (that's the thing my previous question was about). Then the object needs to be removes from the array. If the array is empty, there needs to be a message reading "The array is empty" in red. This is the way I've done it, but there's an error message saying "Uncaught TypeError: Cannot read properties of undefined (reading 'key')" Please help, I wanna know how you would do this.

let globalArray = [];

function add() {
    var key = document.querySelectorAll("input")[0];
    var manufacturer = document.querySelectorAll("input")[1];
    var model = document.querySelectorAll("input")[2];

    var car = {
        key: key.value,
        manufacturer: manufacturer.value,
        model: model.value 
    }
    globalArray.push(car);
}

function fill(){
    if (globalArray.length == 0){
        let msg = document.querySelector("p");
        msg.innerHTML = "The array is empty.";
        msg.style.color = "red";
    }

    else {
        if(globalArray.length % 2 == 0) {
            let list = document.querySelectorAll("ul");
            let li = document.createElement("li");
            li.innerHTML = `${globalArray[globalArray.length / 2 - 1].key} 
${globalArray[globalArray.length / 2 - 1].manufacturer} ${globalArray[globalArray.length / 2 - 1].model}`;
            lista.appendChild(li);
            globalni.splice(globalArray[globalArray.length / 2 - 1], 1);
        }
        else {
            let list = document.querySelectorAll("ul");
            let li = document.createElement("li");
            li.innerHTML = `${globalArray[Math.round(globalArray.length / 2)].key} ${globalArray[Math.round(globalArray.length / 2)].manufacturer} ${globalArray[Math.round(globalArray.length / 2)].model}`
            lista.appendChild(li);
            globalArray.splice(globalArray[Math.round(globalArray.length / 2)], 1);
        }
    }
}
about 4 years ago · Juan Pablo Isaza
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