简体   繁体   中英

Node.js For() loop not working as expected

I am just going to start out by stating I am very fresh to the world of programming.

else if (message.content.startsWith('s!stats defense')){
        const args1 = message.content.slice(16).split();
        const command1 = args1.shift().toLowerCase();
        editedmessage = command1 
        var i = 0;
        for (i = 0; i != 0;){
            let _name = client.defense[editedmessage].Name
            if (error){
                message.channel.send('Player is not in the defensive stats database. If you think it should be DM Toasty')
                i = -1
            } else {
                message.channel.send(_name)
                i = 1
            }
           
            
        }
        message.channel.send(_name)

It skips right over the for() loop. The JSON file is:

{
    "aaron ozark": {
      "Name": "Aaron Ozark",
      "Games": 1,
      "Tackles": 1,
      "Sacks": 0,
      "Passesdefended": 0,
      "Interceptions": 0,
      "ForcedFumbled": 0,
      "FumblesRecovered": 0,
      "Touchdowns": 0,
      "Team": "Vipers",
      "position": "WR",
      "FantasyPoints": 0
    },
    "adelie de pengu": {
      "Name": "Adelie De Pengu",
      "Games": 7,
      "Tackles": 27,
      "Sacks": 1,
      "Passesdefended": 3,
      "Interceptions": 2,
      "ForcedFumbled": 0,
      "FumblesRecovered": 0,
      "Touchdowns": 0,
      "Team": "Wallabies",
      "position": "DB",
      "FantasyPoints": 6.5
    },
    "akashi seijuro": {
      "Name": "Akashi Seijuro",
      "Games": 7,
      "Tackles": 24,
      "Sacks": 1,
      "Passesdefended": 2,
      "Interceptions": 3,
      "ForcedFumbled": 0,
      "FumblesRecovered": 1,
      "Touchdowns": 0,
      "Team": "Aztecs",
      "position": "DB",
      "FantasyPoints": 10
    }
}

The i variable is going to be used later on in the code to determine if the rest of the stats are there. I know I am doing something wrong because when I added the final message.channel.send(_name) outside of the for() loop if alerted me that "_name was not defined."

Your for loop is incorrect. Currently you have

for (i = 0; i != 0;)

You're setting i as 0 and your exit condition for the loop is when i != 0 so your loop doesn't occur cause the exit condition is false initially.

I'd suggest reading through how for loops work: MDN Doc

Also, I don't see why you need a loop as you're not really looping through the anything.


The _name being not defined is because you define _name within the for loop block.

for (...) {
  let _name
}

message.channel.send(_name)

The _name needs to be outside the for loop block.

let _name

for (...) {
  _name = ...
}

message.channel.send(_name)

Overall, I see plenty of problems within the implementation of your usecase. I would suggest that you step back and rethink your code.

for loops execute as long as the second statement is true (it checks before each loop-execution. so

for (...; false; ...)

does not execute at all and

for (...; true; ...)

executes infinitely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM