How to set an alert if something is not in an array once

Hello I am trying to figure out how to set an alert("not a option") only once per onkeyup if the letter pressed is not in a a-z array.

When a correct onkeyup is pressed, then it runs through the array and removes it.
But I can't put it at the end of the function because it will pop up regardless...
And I can't put it in the loop because it will run multiple times.

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

(This is for a hangman game, it works, just trying to spice it up)

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

Answers 1

  • You should use indexOf and according to that handle. And check the userGuesses array if don't want to show the alert again.

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

Related Articles