如果某些内容不在数组中,如何设置警报

时间:2017-02-26 04:52:07

标签: javascript jquery arrays

你好我想知道如果按下的字母不在a-z数组中,每个onkeyup只能设置一次警告(“不是一个选项”)。

当按下正确的onkeyup时,它会在数组中运行并将其删除 但我不能把它放在功能的最后,因为无论如何都会弹出它。
我不能把它放在循环中,因为它会运行多次。

//function that compares the letter inputted to the splitWord array
function checkLetter (letter) {
    //setting to false first to cover any input
    var letterGiven = false;
    //run a loop and compare the letter and splitword  
    for (i = 0; i < numberEmptySpaces; i++) {
        if (splitWord[i] === letter) {
            letterGiven = true;
            //if it is true then letter will equal emptydisplay and replace the "_"        
            emptyDisplay[i] = letter;
            //this updates the emptyDisplay with the letter that was given.      
            $('#randomId').html(emptyDisplay.join(" "));       
        }
    }  
    //if it is not true then you lose one live and the letter is unputted to userGuess array      
    if (!letterGiven) {
        livesRemaining--;
        userGuesses.push(letter);
        $('#wrongWordId').html("[ " + userGuesses.join(", ") + " ]");
        $('#livesId').html(livesRemaining);
    }
    console.log(userGuesses);
    console.log(livesRemaining);      
    //checking to see if the empty display is undated when onkeyup is actived
    console.log(emptyDisplay);      
}

(这是一个刽子手游戏,它可以工作,只是试图加强它)

//function that will only allow a-z and space bar to be pressed
function availableLetters(letter) {
    var letterGiven = false;
    var alphabet = 'abc defghijklmnopqrstuvwxyz'.split('');
    //check to see if it splits when called, it does
    for (i = 0; i < alphabet.length; i++) {
        if (alphabet[i] === letter) {
            letterGiven = true;
            //removes the current letter from the alphabet array      
            alphabet.splice(i, 1);
        }  
    }
}

//---------------------------------------------------------------

//starts the initial game
startUp();

//listens for an event, which is onkeyup
$(document).on("keyup", function(event) {
    //creates a variable and converts into a string
    //fromcharcode is a converts assigned number to a letter and event.which is the number
    //toLowerCase just lower cases any string inputed   
    var keyLetter = String.fromCharCode(event.which).toLowerCase();
    availableLetters(keyLetter);
    checkLetter(keyLetter);
    updateInfo();

1 个答案:

答案 0 :(得分:1)

您应该使用userGuesses并根据该句柄。如果您不想再次显示提醒,请检查//function that compares the letter inputted to the splitWord array function checkLetter (letter) { //setting to false first to cover any input var letterGiven = false; //run a loop and compare the letter and splitword if(splitWord.indexOf(letter)>=0){ letterGiven = true; //if it is true then letter will equal emptydisplay and replace the "_" emptyDisplay[i] = letter; //this updates the emptyDisplay with the letter that was given. $('#randomId').html(emptyDisplay.join(" ")); } //if it is not true then you lose one live and the letter is unputted to userGuess array if (!letterGiven && userGuesses.indexOf(letter)<0) { livesRemaining--; userGuesses.push(letter); $('#wrongWordId').html("[ " + userGuesses.join(", ") + " ]"); $('#livesId').html(livesRemaining); } console.log(userGuesses); console.log(livesRemaining); //checking to see if the empty display is undated when onkeyup is actived console.log(emptyDisplay); } 数组。

<input type="checkbox" name='check'>
   <input type="checkbox" name='check'>
   <input type="checkbox" name='check'>
   <input type="checkbox" name='check'>
   <span id="count"></span>