在循环内进行测试

Testing inside a loop

本文关键字:测试 循环      更新时间:2023-09-26

我正在做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之一是否等于"是",如果是,我如何让玩家知道("您不能指定多个经销商")并将他返回到循环中?

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

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);