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

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

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 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!