Store objects in a array

Trying to make a website for buying movie tickets and store it in an array as well as showing them on screen but I can’t figure out why it is not making an array. when I try to order a second order it overwrites the old object.

JS CODE:

function test() {
  const billett = [];

  let valgtFilm = document.getElementById("film").value;
  let antall = document.getElementById("antall").value;
  let fornavn = document.getElementById("forNavn").value;
  let etternavn = document.getElementById("etterNavn").value;
  let telefon = document.getElementById("telefornr").value;
  let epost = document.getElementById("epost").value;
  let tall = Number(antall);
  let telefonnr = Number(telefon);

  let ut = "<table><tr>" +
    "<th>Film</th><th>antall</th><th>Navn</th><th>Etternavn</th><th>Telefonnr</th><th>Epost</th>" +
    "</tr>";


  if (valgtFilm === "non") {
    alert("Feil1");
  }
  if (tall <= 0) {
    alert("Feil2");
  }
  if (fornavn === "") {
    alert("Feil3");
  }
  if (etternavn === "") {
    alert("Feil4");
  }
  if (isNaN(telefonnr) || telefon === "") {
    alert("Feil5");
  }
  if (epost === "") {
    alert("Feil6");
  } else {
    const nyBillett = {
      film: valgtFilm,
      antall: tall,
      navn: fornavn,
      etternavn: etternavn,
      tlf: telefonnr,
      epost: epost,
    }
    billett.push(nyBillett);
    for (let b of billett) {

      ut += "<tr>";
      ut += "<td>" + b.film + "</td><td>" + b.antall + "</td><td>" + b.navn + "</td><td>" + b.etternavn + "</td><td>" + b.tlf + "</td><td>" + b.epost + "</td>";
      ut += "</tr></br>";

      document.getElementById("feil").innerHTML = ut;

      //fjerner verdier
      document.getElementById("film").value = 'Velg film her';
      document.getElementById("antall").value = "";
      document.getElementById("forNavn").value = "";
      document.getElementById("etterNavn").value = "";
      document.getElementById("telefornr").value = "";
      document.getElementById("epost").value = "";
    }
  }
}

16 thoughts on “Store objects in a array”

Leave a Comment