-1

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);
ref hal
  • 1
  • 2
  • 1
    Welcome to Stack Overflow! Relevant code and error messages need to be included in your question *as text*, [not as pictures of text](https://meta.stackoverflow.com/q/285551/328193). Just linking to screen shots makes it more difficult for people to help you. Please provide a runnable [mcve] which demonstrates the problem and indicate specifically what problem you are observing in your debugging. To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Jul 25 '23 at 19:22
  • Can you describe what you expect that `else` block to actually do and why? It shows an `alert`, then prompts the user for two values but never uses those values for anything, then calls `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, and then the loop just continues again with trying to re-validate the original (still unchanged) values... – David Jul 25 '23 at 19:35
  • PSA: Consider using `let` instead of `var`. – tadman Jul 25 '23 at 19:35
  • I want it to loop the top "FOR" loop 3 times. and say how many atempts do I have left – ref hal Jul 25 '23 at 19:36
  • @refhal: "I want" is not a specific reason for code to do anything at all. The code currently does *exactly* what it was written to do. The question isn't what do you *want* the code to do, but what you *expect* any given specific operation to do and *why*. (For example, I personally *want* to have a million dollars. But the fact that I *don't* have a million dollars is not an indication that something isn't working as expected.) – David Jul 25 '23 at 19:40
  • @refhal: This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? – David Jul 25 '23 at 19:43
  • 1
    One of the basic tools to start immediately with is a debugger. Step through the code and inspect variables. Now ask your self how many times is `isUserValide` executed, what is the use of calling it when you don't inspect its return value? How many times do you want to execute `alert( "Too many atempts");`? What does a `for` loop do when you don't increment the loop variable? – trincot Jul 25 '23 at 19:45

2 Answers2

0

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.
David
  • 208,112
  • 36
  • 198
  • 279
-1
 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):

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 29 '23 at 07:55
  • 1
    ***In this loop, you have error because loop has no condition. So it works endlessly.*** Not exactly right, there is a condition `i >= 0;` there is no increment (or decrement) i.e. the `i++` or `i--`. **for (var i = 3; i >= 0;)** endlessly loops because `i` never changes from value 3 and the criteria is always `True`. – moken Jul 31 '23 at 05:27