Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
Máquina expendedora de JavaScript Verifique si el artículo existe más nuevo y cambio de stock

Estoy construyendo una máquina expendedora en JavaScript para aprenderlo. Pero ahora tengo un problema. Todos los valores son de 3 campos de entrada que entrego. Así que quiero construir una función para verificar si un refrigerio ya existe. Si existe, debe agregar el nuevo precio y el stock existente se agregará con el stock fuera de mi campo de entrada. Si el snack no está en la máquina se añadirá. Esto es lo que ya tengo.

 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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!