在jquery中添加数组中所选元素的值

时间:2016-01-11 18:17:44

标签: jquery arrays sum

我正在制作一个琐事游戏。我有以下问题/答案/分数的示例数组,并且不确定如何仅总结正确猜测的答案的分数,例如在问题1中,如果该人猜到“否”他们得到27分,那么如果他们猜测“坐”,他们总共获得50分(27 + 23)分,依此类推。我想在id为“#family1-score”的文本区域中输入总数。

var currentQuestion = null;

var qData = [
    {question: 'Name a word that most people yell at their dogs', answers: ['no', 'sit', 'stay', 'down', 'fetch', 'jump'], scores: [27,23,14,7,6,5]},

    {question: 'Name a fruit you can eat with one bite.',
      answers: ['strawberry', 'grape', 'blueberry', 'raspberry', 'cherry', 'blackberry'],
      scores: [56,17,9,3,2,1]
    },

    {question: 'Name a holiday when people have parties.',
      answers: ['christmas', 'thanksgiving', 'halloween', '4th of july', 'labor day', 'new years eve'],
      scores: [35,21,11,3,2,1]
    }
];

$(".answerInputButton").click(function(){

    var answerData = $(".answerInput").val()
    //check if answer inputted is in the array of correct answers
    var isInArray = ($.inArray(answerData, qData[currentQuestion].answers) !== -1)
    //get the index of the correct answer from the array
    var indexOfAnswer = qData[currentQuestion].answers.indexOf(answerData)
    //get the score of the answer based on location of index of answer
    var answerScore = qData[currentQuestion].scores[indexOfAnswer]
    //the text of the answer tile when the answer is right
    var corrAnswer = answerData + " " + answerScore

    // console log the answer from the input field
    console.log(currPlayer + " your answer is ", answerData)

    if(isInArray) {
        if (currPlayer === player1Name) {
            answerCount++
            $("#family1-score").text(answerScore)


            console.log(currPlayer + ". You got " + answerCount + " answers right")
            //check if winner
            if (answerCount === 6) {
                gameMsg(currPlayer + " has won this round! " + currPlayer + ", get ready to start the next round")
                //reset board to next question
                //tally points in scorebox
                $("#answerArea").hide()
                $("#question").hide()
            }
        } 
        else if (currPlayer === player2Name) {
            answerCount++
            $("#family2-score").text(answerScore)           
            console.log(currPlayer + ". You got " + answerCount + " answers right")
            //check if winner
            if (answerCount === 6) {
                gameMsg(currPlayer + " has won this round! " + currPlayer + ", get ready to start the next round")
                //reset board to next question

                //tally points in scorebox
                $("#answerArea").hide()
                $("#question").hide()
            }
        }
        //console.log the score that corresponds to the index of the answer
        console.log("you received ", answerScore," points")
        // show green checkmark
        $("#greencheckmark").show().delay(2000).fadeOut()
        //flip corresponding answer tile with the answer and score
        if (indexOfAnswer === 0) {
            $("#frontAnswer1").removeClass("answerTile").addClass("rightAnswer").html(corrAnswer);
        } else if (indexOfAnswer === 1) {
            $("#frontAnswer2").removeClass("answerTile").addClass("rightAnswer").html(corrAnswer);
        } else if (indexOfAnswer === 2) {
            $("#frontAnswer3").removeClass("answerTile").addClass("rightAnswer").html(corrAnswer);
        } else if (indexOfAnswer === 3) {
            $("#frontAnswer4").removeClass("answerTile").addClass("rightAnswer").html(corrAnswer);
        } else if (indexOfAnswer === 4) {
            $("#frontAnswer5").removeClass("answerTile").addClass("rightAnswer").html(corrAnswer);
        } else if (indexOfAnswer === 5) 
            $("#frontAnswer6").removeClass("answerTile").addClass("rightAnswer").html(corrAnswer);
        } else {
            if (currPlayer === player1Name) {
                strikeCount ++
                console.log ('Try again')
                    if (strikeCount === 1) {
                        $("#onestrike").show().delay(2000).fadeOut()
                    } else if (strikeCount === 2) {
                        $("#twostrike").show().delay(2000).fadeOut()
                    } else if (strikeCount === 3) {
                        $("#threestrike").show().delay(2000).fadeOut()
                        //switch turns after a player gets 3 strikes
                        currPlayer = player2Name
                        //reset strike count to 0 for player 2
                        console.log(currPlayer + "'s turn now.")
                        //show message to next player that they can steal points for guessing any answer correctly in 5 seconds
                        gameMsg(currPlayer + ", you have 5 seconds to guess any remaining answer correctly and steal all the points. Get ready!")
                }
        }
        else if (currPlayer === player2Name) {
            strikeCount ++
            console.log ('Try again')
                if (strikeCount === 1) {
                    $("#onestrike").show().delay(2000).fadeOut()
                } else if (strikeCount === 2) {
                    $("#twostrike").show().delay(2000).fadeOut()
                } else if (strikeCount === 3) {
                    $("#threestrike").show().delay(2000).fadeOut()
                    //switch turns after a player gets 3 strikes
                    currPlayer = player1Name
                    //reset strike count to 0 for player 1
                    console.log(currPlayer + "'s turn now.")
                    //show message to next player that they can steal points for guessing any answer correctly in 5 seconds
                    gameMsg(currPlayer + ", you have 5 seconds to guess any remaining answer correctly and steal all the points. Get ready!")
                }
        }
        console.log("number of strikes is ", strikeCount)
    }
})

0 个答案:

没有答案