Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

56
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda