Problem with if condition in javascript

0

I have a problem when executing my code since the idea is that when a product is already registered only the amount and price can be entered, however, entering the same product key instead of leaving that allows me to change the name of the product (which should not happen since that product key can only have one name) because this is for the provider's key and yaesta2 = product key

if(menu==2){
  yaesta=0;
  yaesta2=0;
proveedor1=prompt("Ingrese ID del proveedor")
for(x=1;x<=renglon;x++){
  if(proveedor1===clave[x])
  {
    yaesta=1;
  }
}
if(yaesta===0){
  alert("No se encontró ID")
}

if(yaesta===1)  {
  producto1=prompt("Ingrese clave del producto uwu")

  for(x=1;x<100;x++)
  {
    if(producto1=producto[x])
    {
      yaesta2=1; 
       
    }
  }
}
if(yaesta2===1 && yaesta===1){
  alert("Producto encontrado: "+producto[x]);
  producto[x]=prompt("Ingrese precio de "+producto[x]);
  cantidad[x]=prompt("Ingrese cantidad de "+ producto[x]);
}
if(yaesta2===0 && yaesta===1){
  producto[x]=prompt("Ingrese nombre del producto")
  precio[x]=prompt("Ingrese precio del producto")
  cantidad[x]=prompt("Ingrese cantidad de producto")
}
  
}//Adquisiciones
    
asked by Stephanie 30.06.2017 в 21:28
source

3 answers

3

EDIT: I have looked at the code more thoroughly, and I have found a few failures.
The arrays are traversed from position 0 and not 1
To traverse the arrays, use length to control if you reach the end.
Once you have found the value, you do not have to keep looking. To compare use '==' and not '=' ('===' is to verify that it is the same value and type, it should be used only in specific cases)
You compare the product identifier with the name of the product, so I could not find it.
Once the indicator is entered, it must be saved for future checks.
The price you are saving in product [x] and not in price [x]

I've tweaked the code a bit so you can try it from here.

/* Me invento variables */
	var clave=["1","2","3","4","5"];
	var claveProducto=[]; // array inventada, no se cual usas.
	var producto=[];
	var precio=[];
	var cantidad=[];
/* ********************* */
do {  // Me invento que repite hasta poner un 0 para poder salir.
	yaesta=0;
	yaesta2=0;
	proveedor1=prompt("Ingrese ID del proveedor (0 para salir)");
	if (proveedor1!="0") {
		for(x=0 ; x<clave.length ; x++){	// primer fallo, los arrays se inician en la posicion 0, para recorrer todo el array usa length.
			if(proveedor1===clave[x])
			{
				yaesta=1;
				break; // consejo, una vez encontrado no hace falta seguir buscando.
			}
		}
		if(yaesta===0){
			alert("No se encontró ID")
		}
		if(yaesta===1)  {
			producto1=prompt("Ingrese clave del producto uwu")
			for(x=0 ; x<claveProducto.length ; x++)// Los arrays se inician en la posicion 0, para recorrer todo el array usa length.
			{
				if(producto1==claveProducto[x])	// fallo, para comparar se una '=='
				{								// fallo, estas comparando una "clave" con producto[x] y deberia ser claveProducto[x] (o donde tu lo tengas)
					yaesta2=1; 
					break; // consejo, una vez encontrado no hace falta seguir buscando.
				}
			}
		}
		if(yaesta2===1 && yaesta===1){
			alert("Producto encontrado: "+producto[x]);
			precio[x]=prompt("Ingrese precio de "+producto[x]); // fallo, el precio deberia ir a precio[x]
			cantidad[x]=prompt("Ingrese cantidad de "+ producto[x]);
		}
		if(yaesta2===0 && yaesta===1){
			claveProducto[x]=producto1; // si no existe la id de producto, lo incluye.
			producto[x]=prompt("Ingrese nombre del producto")
			precio[x]=prompt("Ingrese precio del producto")
			cantidad[x]=prompt("Ingrese cantidad de producto")
		}
	}
} while (proveedor1!=0)
    
answered by 30.06.2017 в 21:39
0

When doing a string comparison make sure you parsed the content using

if(String("producto1") == String("producto2"))

also takes into account the difference between operators

if(a===a)

is different from:

if(a==a)

    
answered by 30.06.2017 в 23:45
0

Try this code, I do not know if it was what you wanted but I hope and it will help you. Order it a bit to have a better organization of the products.

// Proveedores de los productos 
let provedores = [[], [], [], [], []]
// Productos
let productos = []

let yaesta = 0
let yaesta2 = 0

do {
  // Almacena el id del proveedor donde se van almacenar los productos
  let proveedorID = prompt("Ingrese ID del proveedor (0 para salir)")
  
  if(proveedorID == 0) break
    
  // Identifica si existe el proveedor
  if(provedores[proveedorID] === undefined){
    // Si no existe te pide que crees uno nuevo.
    if(confirm("No se encontro el ID del provedor. ¿Decea crear uno nuevo?")){
      proveedorID = provedores.length + 1
      provedores[proveedorID-1] = []
      yaesta = 1
    }
  } else {
    yaesta = 1
  }

  if(yaesta == 0) {
    alert("No se encontro el ID")
  } else if(yaesta == 1) {
    
    // Verifica si el array de productos y el de proveedor ya cuenta con productos.
    if(productos.length && provedores[proveedorID-1].length){
    
      let claveProducto = prompt("Ingrese clave del producto")
      
      // Iteramos en el array de productos en el proveedor elegido para saber si la clave obtenida ya existe en un producto. 
      provedores[proveedorID-1].every(producto => {
      
        if(claveProducto == producto.key){
          yaesta2 = 1
          alert("producto encontrado: " + producto.nombre)
          producto.precio = prompt("Ingrese precio del producto " + producto.nombre)
          producto.cantidad = prompt("Ingrese cantidad del producto " + producto.nombre)
          
          return false

        }
        
        return true
      })
      
      if(yaesta2 == 0) create(claveProducto)
      
    } else {
      // Si no crea uno nuevo desde cero.
      create(1)
    }
  }
  
  // Crea nuevos productos.
  function create(keyInit){
    const newProduct = {
      key: keyInit,
      proveedor: proveedorID,
      nombre: prompt("Ingrese nombre del producto"),
      precio: prompt("Ingrese precio del producto"),
      cantidad: prompt("Ingrese cantidad del producto")
    }

    productos.push(newProduct)
    provedores[proveedorID-1].push(newProduct)
  }
  
} while(true)

console.log("Todos los productos: ")
console.log(productos)
console.log("Proveedores: ")
console.log(provedores)
    
answered by 01.07.2017 в 09:15