Question

This is my first attempt at a little more complex code structure. The thing is my IDE says it technically works , jsfiddle says it doesn't, it actually initializes only the two confirm functions that I declared "confirmUserName();" and "confirmFullName();"

Can someone explain why i did such a horrible job.

var userList = [];  
var username = "";   
var fullname = ""; 
var addUserName = addUser(username, userList);        v
var addFullName = addUser(fullname, userList); 
function addUser(usrName, list) {
    if(list.length == 0) {
        list.push(usrName); // userlist empty add the new user
    } else {
        for (var i = 0; i < list.length; i++) {   
            if(list[i] == undefined) {            
                list[i] = usrName;              
                return list;                     
            } else if(i == list.length - 1) {    
                list.push(usrName);             
                return list;                   

            }
        }
    }
}            // Function that adds user and name to list

var usernameQuery;                                  
function confirmUserName() {
   confirm("Is " + username + " your first choice?");

    if (confirmUserName == true) {
        return fullnameQuery;
    } else {
        return usernameQuery;
    }
}                   
var fullnameQuery;                                   /
function fullnameConfirm() {
    confirm("Is " + fullname + " your first choice ");
    if (fullnameConfirm == true) {
        return  startRide;
    } else {
        return fullnameQuery;
    }
}                   
if(username == undefined) {
    usernameQuery = function() {
      username =  prompt("You are the first user to play, \n" +
               " Chose and let the game begin !");
      return addUserName;
    };
} else {
    usernameQuery = function() {
        username = prompt("What username whould you like to have ? \n" +
            " Chose and let the game begin !");
        return addUserName;
    };
}
confirmUserName();
if(fullname == undefined)  {
    fullnameQuery = function() {
        fullname = prompt("Enter your real name !");
        return addFullName;
    };

    } else {
    fullnameQuery = function() {
        fullname = prompt("Enter your real name!");
    return addFullName;

    };
}
fullnameConfirm();
Was it helpful?

Solution

There is a lot wrong with the code you posted -- I'll just take one chunk:

function confirmUserName() {
   // The return value of `confirm` is ignored.
   confirm("Is " + username + " your first choice?");

    // confirmUserName is the name of your function.
    // You sould be using `===` instead of `==`
    // Or, not comparing against true at all.
    if (confirmUserName == true) {
        return fullnameQuery;
    } else {
        return usernameQuery;
    }
}   

Fixed function:

function confirmUserName() {
   var res = confirm("Is " + username + " your first choice?");
    if (res) {
        return fullnameQuery;
    } else {
        return usernameQuery;
    }
}

OTHER TIPS

It does not throw Errors with this, but I dont know in which situation you want your code to be implemented and what it should do, I hope this is what you need:

var userList = [];
var username = "";
var fullname = "";
var addUserName = addUser(username, userList);

var addFullName = addUser(fullname, userList);

function addUser(usrName, list) {
    if (list.length === 0) {
        list.push(usrName); // userlist empty add the new user
    } else {
        for (var i = 0; i < list.length; i++) {
            if (list[i] === undefined) {
                list[i] = usrName;
                return list;
            } else if (i == list.length - 1) {
                list.push(usrName);
                return list;

            }
        }
    }
} // Function that adds user and name to list

var usernameQuery;

function confirmUserName() {
    confirm("Is " + username + " your first choice?");

    if (confirmUserName === true) {
        return fullnameQuery;
    } else {
        return usernameQuery;
    }
}
var fullnameQuery;

function fullnameConfirm() {
    confirm("Is " + fullname + " your first choice ");
    if (fullnameConfirm === true) {
        return startRide;
    } else {
        return fullnameQuery;
    }
}
if (username === undefined) {
    usernameQuery = function () {
        username = prompt("You are the first user to play, \n" +
            " Chose and let the game begin !");
        return addUserName;
    };
} else {
    usernameQuery = function () {
        username = prompt("What username whould you like to have ? \n" +
            " Chose and let the game begin !");
        return addUserName;
    };
}
confirmUserName();
if (fullname === undefined) {
    fullnameQuery = function () {
        fullname = prompt("Enter your real name !");
        return addFullName;
    };

} else {
    fullnameQuery = function () {
        fullname = prompt("Enter your real name!");
        return addFullName;

    };
}
fullnameConfirm();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top