Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

261
Vistas
JavaScript Vending Machine Check if Item exist else new and stock change

I m building a vending machine in JavaScript to learn it. But now I have a problem. All values are from 3 input fields which i hand over. So I want to build a function to check If a snack is in already existing. If it's exist it should add the new price and the existing stock will added with the stock out of my input field. If the snack is not in the machine it will be added. This is what I already have.

 var selection = [
        {name: "fanta", price: 1.60, stock: 4},
        {name: "cola", price: 1.70, stock: 3},
        {name: "nuts", price: 1.10, stock: 8},
        {name: "mars", price: 1.20, stock: 2},
        {name: "snickers", price: 1.20, stock: 5}
    ];




function new_snack_snack(selection){


var new_price_input = document.getElementById("new_price").value;
var new_stock_input = document.getElementById("new_stock").value;
var new_snack_input = document.getElementById("new_snack").value;


selection.push({name: new_snack_input, price: new_price_input, stock: new_stock_input});

console.log(selection);

    document.getElementById("snack_table_output").innerHTML = create_table();

    }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

var selection = [{
    name: "fanta",
    price: 1.60,
    stock: 4
  },
  {
    name: "cola",
    price: 1.70,
    stock: 3
  },
  {
    name: "nuts",
    price: 1.10,
    stock: 8
  },
  {
    name: "mars",
    price: 1.20,
    stock: 2
  },
  {
    name: "snickers",
    price: 1.20,
    stock: 5
  }
];




function new_snack_snack(selection) {


  var new_price_input = document.getElementById("new_price").value;
  var new_stock_input = document.getElementById("new_stock").value;
  var new_snack_input = document.getElementById("new_snack").value;


  if (!selection.find(item => item.name === new_snack_input) {
      selection.push({
        name: new_snack_input,
        price: new_price_input,
        stock: new_stock_input
      });
    }

    console.log(selection);

    document.getElementById("snack_table_output").innerHTML = create_table();

  }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda