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

57
Vistas
Array element values changing without reason

I am trying to make a discord bot for my server (a currency bot) and I am trying to implement a trading system, but the arrays I am using for the traded items have items whose values change randomly. I haven't changed the arrays value anywhere else but the values are still changing. Does anyone know why? The code parses inputs like this: 3apple,1stick 1axe .

trade.ts

youend = [];
themend = [];
var temp: Item | undefined;
for (var i = 0; i < you.length; i++) {
  const item = you[i];
  for (let i = 0; i < ITEMS.length; i++) {
    const item2 = ITEMS[i];
    temp = undefined;
    if (
      reAll(
        item
          .slice(/[a-z]/i.exec(reAll(item.toLowerCase(), "_", " "))?.index)
          .toLowerCase(),
        "_",
        " "
      ) === item2.name.toLowerCase()
    ) {
      let temp = item2;
      //@ts-ignore
      temp.amount = parseInt(
        item.slice(
          0,
          /[a-z]/i.exec(reAll(item.toLowerCase(), "_", " "))?.index + 1
        )
      );
      youend.push(temp);
      console.log(JSON.stringify(temp), youend, themend);
    }
  }
}
temp = undefined;
for (var i = 0; i < them.length; i++) {
  const item = them[i];
  for (let i = 0; i < ITEMS.length; i++) {
    const item2 = ITEMS[i];
    temp = undefined;
    if (
      reAll(
        item
          .slice(/[a-z]/i.exec(reAll(item.toLowerCase(), "_", " "))?.index)
          .toLowerCase(),
        "_",
        " "
      ) === item2.name.toLowerCase()
    ) {
      let temp = item2;
      //@ts-ignore
      temp.amount = parseInt(
        item.slice(
          0,
          /[a-z]/i.exec(reAll(item.toLowerCase(), "_", " "))?.index + 1
        )
      );
      themend.push(temp);
      console.log(JSON.stringify(temp), youend, themend);
    }
  }
}

The item class

export class Item {
    name: string
    icon: string
    sell: number
    durability: number
    amount: number
    constructor(name: string, icon: string, sell: number, durability: number, amount: number) {
        this.name = name
        this.icon = icon
        this.sell = sell
        this.durability = durability
        this.amount = amount
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I'm not sure if this is the solution, but it looks like you are using two i variables in the for loops.


In both of your for loops, you are defining two i variables, using var and let.

for (var i = 0; i < arr.length; i++) {
  for (let i = 0; i < arrTwo.length; i++) {
    
  }
}

This shouldn't work correctly, so a good fix for it is to simply change both of them to different variable names.

for (var i = 0; i < arr.length; i++) {
  for (let j = 0; i < arrTwo.length; i++) {
    
  }
}

Also, you shouldn't use both var and let. You should just stick to one.

for (let i = 0; i < arr.length; i++) {
  for (let j = 0; i < arrTwo.length; i++) {
    
  }
}

These changes might fix your code.


In conclusion, the solution might be to change the variable names.

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