有人可以解释这个功能/参数吗?

时间:2019-09-19 23:17:05

标签: javascript function arguments

我了解该函数的功能,但我不理解其功能的原因。具体而言,我不了解checkLetters函数(字母)和(猜测)的参数,其工作原理以及如何在if / else语句中使用它来确定所选字母是否正确。 “字母”不是变量,仅在函数中作为参数提及,并且仍按以下方式使用:

randomWord[i] == letter
blanksAndCorrect[i] = letter;
checkLetters(guesses);

参数是否已转换为变量?我要引用的代码在以下部分的Javascript文本中://调用启动游戏功能和//检查字母/比较功能。

//VARIABLES
var words = ["arthur", "rugrats", "thesimpsons", "scoobydoo", "spongebob", "dannyphantom", "teentitans"]

//Empty variables to store values later
var randomWord = "";
var lettersOfWord = []
var blanks = 0;
var blanksAndCorrect = [];
var wrongGuess = [];

//Counter Variables
var wins = 0;
var losses = 0;
var guessesRemaining = 9;



// ALL FUNCTIONS
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


//__________________________________________________________
//GAME START FUNCTION
//__________________________________________________________
function Game() {
    //computer generates random word from words array
    randomWord = words[Math.floor(Math.random() * words.length)];

    // split the individual word into separate arrays, and store in new array 
    lettersOfWord = randomWord.split("");

    //store length of word in blanks, for later use
    blanks = lettersOfWord.length;

    //creating a loop to generate "_" for each letter in array stored in blanks
    for (var i = 0; i < blanks; i++) {
        blanksAndCorrect.push("_");
    }

    //showing the "_" within HTML
    document.getElementById("currentword").innerHTML = "  " + blanksAndCorrect.join("  ");

    //console logging 
    console.log(randomWord);
    console.log(lettersOfWord)
    console.log(blanks)
    console.log(blanksAndCorrect)
}


//__________________________________________________________
//AUDIO FUNCTION
//__________________________________________________________

//variables for audio function
var a = document.getElementById("arthur");
var r = document.getElementById("rugrats");
var simpsons = document.getElementById("simpsons");
var scoobydoo = document.getElementById("scoobydoo");
var spongebob = document.getElementById("spongebob");
var danny = document.getElementById("danny");
var teent = document.getElementById("teent");



//__________________________________________________________
//RESET FUNCTION
//__________________________________________________________
function reset() {
    guessesRemaining = 9;
    wrongGuess = [];
    blanksAndCorrect = [];
    Game()
}

//__________________________________________________________
//CHECK LETTERS/COMPARE FUNCTION
//__________________________________________________________

//If/Else, to see if letter selected matches random word
function checkLetters(letter) {
    var letterInWord = false;
    //if the generated randomword is equal to the letter entered... then variable is true
    for (var i = 0; i < blanks; i++) {
        if (randomWord[i] == letter) {
            letterInWord = true;
        }
    }
    //if letterInWord (false)
    if (letterInWord) {
        //check each letter to see if it matches word
        for (var i = 0; i < blanks; i++) {
            if (randomWord[i] == letter) {
                blanksAndCorrect[i] = letter;
                console.log(blanksAndCorrect[i])
            }
        }
    }
    //otherwise, push the incorrect guess in the wrong guesses section, and reduce remaining guesses
    else {
        wrongGuess.push(letter);
        guessesRemaining--;
    }
    console.log(blanksAndCorrect);
}

//__________________________________________________________
//FINAL COMPLETE FUNCTION
//__________________________________________________________

//check to see if player won...
function complete() {
    console.log("wins:" + wins + "| losses:" + losses + "| guesses left:" + guessesRemaining)

    //if WON...then alert, play audio, display image and reset new round
    if (lettersOfWord.toString() == blanksAndCorrect.toString()) {
        wins++;
        aud()
        reset()
        //display wins on screen
        document.getElementById("winstracker").innerHTML = " " + wins;

        //if LOST...then alert and reset new round
    } else if (guessesRemaining === 0) {
        losses++;
        reset()
        document.getElementById("image").src = "./assets/images/try-again.png"
        document.getElementById("losstracker").innerHTML = " " + losses;
    }
    //display losses on screen && guesses remaining countdown
    document.getElementById("currentword").innerHTML = "  " + blanksAndCorrect.join(" ");
    document.getElementById("guessesremaining").innerHTML = " " + guessesRemaining;
}


//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//_____________________________________________________
// EXECUTE CODE 
//_____________________________________________________

//call start game function
Game()

//check for keyup, and convert to lowercase then store in guesses
document.onkeyup = function (event) {
    var guesses = String.fromCharCode(event.keyCode).toLowerCase();
    //check to see if guess entered matches value of random word
    checkLetters(guesses);
    //process wins/loss 
    complete();
    //store player guess in console for reference 
    console.log(guesses);

    //display/store incorrect letters on screen
    document.getElementById("playerguesses").innerHTML = "  " + wrongGuess.join(" ");
}
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>CHILDHOOD SHOWS GAME</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>

    <body>
        <!--Main Heading-->
        <div class="heading">
            <h1>CHILDHOOD SHOWS</h1>
        </div>

        <div class="background">
            <div class="container">

                <!--Changing Image based on correct answers-->
                <h2>Press Any Key To Guess This Word!</h2>
                
                <!--Current Word Container-->
                <div>
                    <h3 id="currentword">_ _ _ _ _</h3>
                </div>
                <div id="scoreboard">
                    <!--Guesses left Container-->
                    <div class="sidebar">
                        <h3>GUESSES LEFT:
                            <span id="guessesremaining"></span>
                        </h3>
                    </div>

                    <!--Wins counter Container-->
                    <div class="sidebar">
                        <h3>WINS:
                            <span id="winstracker"></span>
                        </h3>
                    </div>
                    <!--Loss counter Container-->
                    <div class="sidebar">
                        <h3>LOSSES:
                            <span id="losstracker"></span>
                        </h3>
                    </div>

                    <!--Wrong guesses container-->
                    <div class="sidebar">
                        <h3>LETTERS YOU GUESSED WRONG:
                            <span id="playerguesses"></span>
                        </h3>
                    </div>
                </div>
            </div>
        </div>



        


        <script src="./game.js"></script>

    </body>

</html>

0 个答案:

没有答案