0

So I'm trying things out. This code should make a useraccount and push the data in an array.

However I cannot seem to run addNewAccount(). It should run automaticly but it doesn't. Probably because i'm doing something wrong. When I type addNewAccount() manually in the console, it returns undefined.

There might be a few bugs since the code is not finished yet but at least addNewAccount() should run right?

//should run code automaticly.
addNewAccount();
let users = [];

function addNewAccount() {

    function userName() {
        let username = prompt('Enter a new username...');
        if (username === '') {
            alert('You must enter a username!')
            userName()
        } else {
            passWord();
        }
    }

    function passWord() {
        let password = prompt('Now enter a new password. It should contain atleast 5 characters...');
        if (password.length < 5) {
            alert('Password must be atleast 5 characters long!')
            passWord();
        } else {
            confirm();
        }
    }

    function confirm() {

        let userInput = prompt(`Username: ${this.username} and Password: ${this.password}. Is this correct? (Y/N)`).toLowerCase();
        if (userInput === 'y') {
            function newUser(x, y) {
                this.username = x;
                this.password = y;
            }
            let user = new newUser(x, y);
            users.push(user);
            alert('Account created!');
        } else if (userInput === 'n') {
            inputN();
            function inputN() {
                let userInput = prompt('Type one of these commands "1" to change username. "2" to change password.');
                if (userInput === '1') {
                    userNameChange();
                    function userNameChange() {
                        let username = prompt('Enter a new username...');
                        if (username === '') {
                            alert('You must enter a username!')
                            userNameChange();
                        } else {
                            confirm();
                        }
                    }
                } else if (userInput === '2') {
                    passWordChange();
                    function passWordChange() {
                        let password = prompt('Now enter a new password. It should contain atleast 5 characters...');
                        if (password.length < 5) {
                            alert('Password must be atleast 5 characters long!')
                            passWordChange();
                        } else {
                            confirm();
                        }
                    }
                } else {
                    alert('Input invalid! Please use one of the commands...')
                    confirm();
                }
            }
        }
    }
}
T.J. Crowder
  • 959,406
  • 173
  • 1,780
  • 1,769
Thoms
  • 1
  • 3
  • `addNewAccount` runs, but it doesn't do anything except create functions it never calls, so you never see it do anything. You see `undefined` in the console because `addNewAccount` has no return value (which is fine if you don't need it to return anything). But there's another issue: If it *did* do something, it would probably have an error because it's run **before** `users` is accessible, because you're calling it before the statement declaring `users`. `users` is inaccessible until the step-by-step execution reaches the `let`. You probably want `let users = [];` above `addNewAccount();`. – T.J. Crowder Feb 21 '22 at 09:33

0 Answers0