Issue
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
}
}
Solution
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.
Answered By - Arnav Thorat
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.