dimanche 10 octobre 2021

Condition is false, but still executed

I have this code

const prompt = require("prompt-sync")({ sigint: true });
var cmd
var start = 0;
var stats = {
                "player1":
                    {
                        "name": "",
                        "money": 0,
                        "properties": [],
                        "position": 0,
                    },
                "player2":
                    {
                        "name": "",
                        "money": 0,
                        "properties": [],
                        "position": 0,
                    },
                "player3":
                    {
                        "name": "",
                        "money": 0,
                        "properties": [],
                        "position": 0,
                    },
                "player4":
                    {
                        "name": "",
                        "money": 0,
                        "properties": [],
                        "position": 0,
                    },
                "player5":
                    {
                        "name": "",
                        "money": 0,
                        "properties": [],
                        "position": 0,
                    }
            }

cmd = prompt("There can be 5 players. Enter the first player's name: ")
stats.player1.name = cmd;
cmd = prompt("Enter the second player's name: ")
stats.player2.name = cmd;
cmd = prompt("Enter the third player's name. Type 0 to start: ")
if (cmd != 0) {
        stats.player3.name = cmd;
    } else {
        start = true;
    }

cmd = prompt("Enter the fourth player's name. Type 0 to start: ")
if (start != true) {
    if (cmd != 0) {
        stats.player4.name = cmd;
    } else {
        start = true;
    }
}

cmd = prompt("Enter the fifth player's name, Type 0 to start: ")
if (start != true) {
    if (cmd != 0) {
        stats.player5.name = cmd;
    } else {
        start = true;
    }
}
while (start = true) {
    cmd = prompt("Enter a command: ")
}

But when I get to the "Enter the third player's name. Type 0 to start" if statement, and type 0, it gets pass this statement but stops at 4th statement. I expected that when I type 0, it will start the game automatically. I used prompt-sync for user input. I'm bad at loops; please help me =[

Aucun commentaire:

Enregistrer un commentaire