skip to Main Content

Hi I just started learning JS and I gut stuck on this part of the loop:

I cant find a way to make it loop 3 times and then block you. or open the site after once or 2 times that i do the wrong password.

it always just does one of this tow (depending on how I change the code)

1:
Enter your username:
+
Enter your password:

Then it loops 3 times

you have 3 more atempts…
you have 2 more atempts…
you have 1 more atempts…

Then it loops forever
"You have no more atempts"

2:
Enter your username:
+
Enter your password:

Then it loops 3 times

you have 3 more atempts…
Enter your username:
+
Enter your password:
you have 2 more atempts…
Enter your username:
+
Enter your password:
you have 1 more atempts…
Enter your username:
+
Enter your password:

Then it loops forever
"You have no more atempts"

var database = [
    {
        username: "refael",
        password: "123456"
    },
    {
        username: "boby",
        password: "ilovepizza"
    },
    {
        username: "sally",
        password: "123"
    }
];

var newsfeed = [
    {
        username: "bob",
        timeline: "i love pizza"
    },
    {
        username: "andy",
        timeline: "im at the pool!"
    }
];

function isUserValide(username, password){
    for (var i=0; i < database.length; i++){
        if (database[i].username === username && 
            database[i].password === password) {
            return true;
        }
    }
    return false;
}

function singin(username, password) {
    for (var atempts = 2; atempts >= 0; atempts--) {
        if (isUserValide(username, password)) {
            console.log(newsfeed);
            alert("SuccessFully Logged In");
            return true;
        } else if (atempts==0) {
            for (var i = 3; i >= 0;) {
                alert("Too many atempts");
            }
        } else {
            alert("You have "+atempts+" atempts left")
            var usernameprompt = prompt("Enter your username")
            var passwordprompt = prompt("Enter your password")
            isUserValide(username, password)
        }
    }
}

var usernameprompt = prompt("Enter your username")
var passwordprompt = prompt("Enter your password")

singin(usernameprompt, passwordprompt);

2

Answers


  1. This else block isn’t actually doing anything:

    else {
        alert("You have "+atempts+" atempts left")
        var usernameprompt = prompt("Enter your username")
        var passwordprompt = prompt("Enter your password")
        isUserValide(username, password)
    }
    

    Its logic is:

    1. Show an alert
    2. Prompt the user for two values but never use those values for anything
    3. Call isUserValide on the same original values (even though we already know the result, since those values haven’t changed) but never uses the result of that function
    4. Then the loop just continues again with trying to re-validate the original (still unchanged) values

    I suspect you meant to update those values before going through the loop again?:

    else {
        alert("You have "+atempts+" atempts left")
        username = prompt("Enter your username")
        password = prompt("Enter your password")
    }
    

    As an aside…

    1. I highly recommend using semi-colons. Relying on automatic semi-colon insertion is a recipe for bugs and other unexpected behaviors.
    2. I also recommend starting to use let and const for more intuitive control over your variable scopes and assignments.
    Login or Signup to reply.
  2.  for (var i = 3; i >= 0;) {
                    alert("Too many atempts");
     }
    

    In this loop, you have error because loop has no condition. So it works endlessly.

    You must write:

    for (var i = 0; i != 3; i++) {
                    alert("Too many atempts");
    }
    

    I’m not a JS programmer, but I think this is correct):

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search