在循环内测试

时间:2015-02-02 18:35:04

标签: javascript

我正在做JS练习(没有jQuery或任何其他JS库),我应该创建一个简单的游戏,开始时选择玩家数量,每个玩家的名字以及玩家是否是经销商。每场比赛只能有一位经销商:

var playersArray = [];
var playerNumber = prompt('Number of players?');

function player(playerName, playerDealer) //creating player object
{
        this.playerName = playerName;
        this.playerDealer = playerDealer; 
    }

for (i=0;i < playerNumber; i++) //creating instances of player object and adding them into playersArray[]
{
    var j = i + 1;
    var inputName = prompt('Name of player '+j);
    var inputDealer = prompt ('Is '+inputName+' also dealer? (yes/no)');
        playersArray[i] = new player(inputName, inputDealer);     
}

我需要在循环中加入一个简单的测试,以检查经销商是否已被任命。我怎么做?我如何遍历“玩家”的所有实例,检查其中一个属性playersArray[].playerDealer是否相等'是'如果是,那么我怎样才能让玩家知道('你不能指定一个以上的经销商' )并让他回到循环中?

1 个答案:

答案 0 :(得分:1)

叹息......至少你试过了。我做了一些微小的调整,以便更容易解析,并添加了一些东西作为用户输入的故障(用户是不可预测的,可能输入你不希望他们输入的东西,所以这些确保你得到一个正确的结束结果)。我还在评论中添加了一些更好的注释,所以一定要看看。

var playersArray = [];
/* We want to be sure the user enters a number here, 
 * so we start by entering a non-number 
 * as our value so we can check later if it now IS a number. */
var playerNumber = "not-yet-determined";
/* Now let's use a while loop! While the following expression
 * evaluates 'false' (and NaN (not-a-number) is considered false!),
 * keep prompting the user to enter a number. */
while(!Number(playerNumber)){
    playerNumber = prompt('Number of players? (numerical)');
}

function player(playerName, playerDealer){
    this.playerName = playerName;
    /* We would like the following to be a boolean, 
     * because its either on or off. 
     * It might actually be better to use 
     * `playerDealer.indexOf("y") >= 0` so the user can also answer with
     * 'y' instead of typing the entire word, but I digress.
     * It might also be best to use .toLowerCase() on the word
     * to avoid some pedantic users typing 'Yes'. But again, I digress. */
    this.playerDealer = playerDealer == "yes" ? true : false; 
}

for (i = 0; i < playerNumber; i++){
    var inputName = prompt('Name of player #'+ (i + 1));
    /* Lets check if the dealer is already set.
     * We do this by creating a variable that will be stored
     * right here. We could also store the variable outside the
     * function (globally) and instead of looping below here, just
     * flip it to true when the dealer get assigned. */
    var dealerAssigned = false;
    /* Then we start looping (with a different variable than i!)) */
    for(var o = 0; o < playersArray.length; o++){
        /* If theres an instance of playerDealer that evaluates to
         * 'true', then assign true to our variable. */
        if(playersArray[o].playerDealer){
            dealerAssigned = true;
            /* Because we know there is only one dealer, 
             * break the loop here. This is to prevent the 
             * loop from continuing, which is good practise since
             * a very large loop can hang your system for a while. */
            break;
        }
    }
    /* Lets only ask if the dealer is assigned when 
     * we know you can assign him. Otherwise we'll default to no.
     * However, since you want to let the user know, you could
     * add the following line: 
     * if(dealerAssigned){
     *     alert("Dealer is already assigned and cannot be reassigned!");
     * }
     */
    var inputDealer = !dealerAssigned
       ? prompt ('Is ' + inputName + ' also dealer? (yes/no)')
       : "no";
    /* Just for the record, you might want to consider using 
     * `push()` here: 
     * playersArray.push(new player(inputName, inputDealer));*/
    playersArray[i] = new player(inputName, inputDealer);     
}
/* Lets verify everything worked. */
console.log(playersArray);