javascript - 我对 Java 脚本缺少什么,请帮助我?

标签 javascript

我有一个html版本的游戏,包含一些java脚本代码,有一个问题和4个答案选项,其中一个是正确的,有一条生命线询问观众给出百分比,但代码的来源总是给出随机百分比,我希望它总是给出正确的答案,我应该做什么?

function calculateGraphPercentages(){
var percentageOfDifficulty = (window.GameVariables.QuestionLevel - 1) * 5;
var beDevious = (Math.random() * 100) > 90 ? true : false;

if(window.GameVariables.AnswerAIsOut == true){
    window.GameVariables.AskTheAudienceVotingPercents[0] = 0;
}

if(window.GameVariables.AnswerBIsOut == true){
    window.GameVariables.AskTheAudienceVotingPercents[1] = 0;
}

if(window.GameVariables.AnswerCIsOut == true){
    window.GameVariables.AskTheAudienceVotingPercents[2] = 0;
}

if(window.GameVariables.AnswerDIsOut == true){
    window.GameVariables.AskTheAudienceVotingPercents[3] = 0;
}

if(beDevious != true){
    if(window.GameVariables.CurrentCorrectAnswer == "A" && window.GameVariables.AnswerAIsOut == false){
        window.GameVariables.AskTheAudienceVotingPercents[0] = Math.ceil(Math.random() * (100 - percentageOfDifficulty));
        window.GameVariables.AskTheAudienceVotingPercents[1] = window.GameVariables.AnswerBIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[0]));
        window.GameVariables.AskTheAudienceVotingPercents[2] = window.GameVariables.AnswerCIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[0] - window.GameVariables.AskTheAudienceVotingPercents[1]));
        window.GameVariables.AskTheAudienceVotingPercents[3] = window.GameVariables.AnswerDIsOut == true ? 0 : 100 - window.GameVariables.AskTheAudienceVotingPercents[0] - window.GameVariables.AskTheAudienceVotingPercents[1]  - window.GameVariables.AskTheAudienceVotingPercents[2];
    }

    if(window.GameVariables.CurrentCorrectAnswer == "B" && window.GameVariables.AnswerBIsOut == false){
        window.GameVariables.AskTheAudienceVotingPercents[1] = Math.ceil(Math.random() * (100 - percentageOfDifficulty));
        window.GameVariables.AskTheAudienceVotingPercents[2] = window.GameVariables.AnswerCIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[1]));
        window.GameVariables.AskTheAudienceVotingPercents[3] = window.GameVariables.AnswerDIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[1] - window.GameVariables.AskTheAudienceVotingPercents[2]));
        window.GameVariables.AskTheAudienceVotingPercents[0] = window.GameVariables.AnswerAIsOut == true ? 0 : 100 - window.GameVariables.AskTheAudienceVotingPercents[1] - window.GameVariables.AskTheAudienceVotingPercents[2]  - window.GameVariables.AskTheAudienceVotingPercents[3];
    }

    if(window.GameVariables.CurrentCorrectAnswer == "C" && window.GameVariables.AnswerCIsOut == false){
        window.GameVariables.AskTheAudienceVotingPercents[2] = Math.ceil(Math.random() * (100 - percentageOfDifficulty));
        window.GameVariables.AskTheAudienceVotingPercents[3] = window.GameVariables.AnswerDIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[2]));
        window.GameVariables.AskTheAudienceVotingPercents[0] = window.GameVariables.AnswerAIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[2] - window.GameVariables.AskTheAudienceVotingPercents[3]));
        window.GameVariables.AskTheAudienceVotingPercents[1] = window.GameVariables.AnswerBIsOut == true ? 0 : 100 - window.GameVariables.AskTheAudienceVotingPercents[2] - window.GameVariables.AskTheAudienceVotingPercents[3]  - window.GameVariables.AskTheAudienceVotingPercents[0];
    }

    if(window.GameVariables.CurrentCorrectAnswer == "D" && window.GameVariables.AnswerDIsOut == false){
        window.GameVariables.AskTheAudienceVotingPercents[3] = Math.ceil(Math.random() * (100 - percentageOfDifficulty));
        window.GameVariables.AskTheAudienceVotingPercents[0] = window.GameVariables.AnswerAIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[3]));
        window.GameVariables.AskTheAudienceVotingPercents[1] = window.GameVariables.AnswerBIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[3] - window.GameVariables.AskTheAudienceVotingPercents[0]));
        window.GameVariables.AskTheAudienceVotingPercents[2] = window.GameVariables.AnswerCIsOut == true ? 0 : 100 - window.GameVariables.AskTheAudienceVotingPercents[3] - window.GameVariables.AskTheAudienceVotingPercents[0]  - window.GameVariables.AskTheAudienceVotingPercents[1];
    }
}
else{
    if(window.GameVariables.AnswerAIsOut == false){
        window.GameVariables.AskTheAudienceVotingPercents[0] = Math.ceil(Math.random() * (100));
        window.GameVariables.AskTheAudienceVotingPercents[1] = window.GameVariables.AnswerBIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[0]));
        window.GameVariables.AskTheAudienceVotingPercents[2] = window.GameVariables.AnswerCIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[0] - window.GameVariables.AskTheAudienceVotingPercents[1]));
        window.GameVariables.AskTheAudienceVotingPercents[3] = window.GameVariables.AnswerDIsOut == true ? 0 : 100 - window.GameVariables.AskTheAudienceVotingPercents[0] - window.GameVariables.AskTheAudienceVotingPercents[1]  - window.GameVariables.AskTheAudienceVotingPercents[2];
    }

    if(window.GameVariables.AnswerBIsOut == false){
        window.GameVariables.AskTheAudienceVotingPercents[1] = Math.ceil(Math.random() * (100));
        window.GameVariables.AskTheAudienceVotingPercents[2] = window.GameVariables.AnswerCIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[1]));
        window.GameVariables.AskTheAudienceVotingPercents[3] = window.GameVariables.AnswerDIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[1] - window.GameVariables.AskTheAudienceVotingPercents[2]));
        window.GameVariables.AskTheAudienceVotingPercents[0] = window.GameVariables.AnswerAIsOut == true ? 0 : 100 - window.GameVariables.AskTheAudienceVotingPercents[1] - window.GameVariables.AskTheAudienceVotingPercents[2]  - window.GameVariables.AskTheAudienceVotingPercents[3];
    }

    if(window.GameVariables.AnswerCIsOut == false){
        window.GameVariables.AskTheAudienceVotingPercents[2] = Math.ceil(Math.random() * (100));
        window.GameVariables.AskTheAudienceVotingPercents[3] = window.GameVariables.AnswerDIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[2]));
        window.GameVariables.AskTheAudienceVotingPercents[0] = window.GameVariables.AnswerAIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[2] - window.GameVariables.AskTheAudienceVotingPercents[3]));
        window.GameVariables.AskTheAudienceVotingPercents[1] = window.GameVariables.AnswerBIsOut == true ? 0 : 100 - window.GameVariables.AskTheAudienceVotingPercents[2] - window.GameVariables.AskTheAudienceVotingPercents[3]  - window.GameVariables.AskTheAudienceVotingPercents[0];
    }

    if(window.GameVariables.AnswerDIsOut == false){
        window.GameVariables.AskTheAudienceVotingPercents[3] = Math.ceil(Math.random() * (100));
        window.GameVariables.AskTheAudienceVotingPercents[0] = window.GameVariables.AnswerAIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[3]));
        window.GameVariables.AskTheAudienceVotingPercents[1] = window.GameVariables.AnswerBIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[3] - window.GameVariables.AskTheAudienceVotingPercents[0]));
        window.GameVariables.AskTheAudienceVotingPercents[2] = window.GameVariables.AnswerCIsOut == true ? 0 : 100 - window.GameVariables.AskTheAudienceVotingPercents[3] - window.GameVariables.AskTheAudienceVotingPercents[0]  - window.GameVariables.AskTheAudienceVotingPercents[1];
    }
}

var sumOfAllPercents = window.GameVariables.AskTheAudienceVotingPercents[0] + window.GameVariables.AskTheAudienceVotingPercents[1] + window.GameVariables.AskTheAudienceVotingPercents[2] + window.GameVariables.AskTheAudienceVotingPercents[3];

if(sumOfAllPercents < 100){
    if(window.GameVariables.CurrentCorrectAnswer == "A" && window.GameVariables.AnswerAIsOut == false){
        window.GameVariables.AskTheAudienceVotingPercents[0] += 100 - sumOfAllPercents;
    }

    if(window.GameVariables.CurrentCorrectAnswer == "B" && window.GameVariables.AnswerBIsOut == false){
        window.GameVariables.AskTheAudienceVotingPercents[1] += 100 - sumOfAllPercents;
    }

    if(window.GameVariables.CurrentCorrectAnswer == "C" && window.GameVariables.AnswerCIsOut == false){
        window.GameVariables.AskTheAudienceVotingPercents[2] += 100 - sumOfAllPercents;
    }

    if(window.GameVariables.CurrentCorrectAnswer == "D" && window.GameVariables.AnswerDIsOut == false){
        window.GameVariables.AskTheAudienceVotingPercents[3] += 100 - sumOfAllPercents;
    }
}

window.GameVariables.AskTheAudienceVotingPercents[0] = window.GameVariables.AskTheAudienceVotingPercents[0]/100;
window.GameVariables.AskTheAudienceVotingPercents[1] = window.GameVariables.AskTheAudienceVotingPercents[1]/100;
window.GameVariables.AskTheAudienceVotingPercents[2] = window.GameVariables.AskTheAudienceVotingPercents[2]/100;
window.GameVariables.AskTheAudienceVotingPercents[3] = window.GameVariables.AskTheAudienceVotingPercents[3]/100;

最佳答案

在重构这样的代码时我会遵循这种模式......

function calculateGraphPercentages() {
    var percentageOfDifficulty = (window.GameVariables.QuestionLevel - 1) * 5;
    var beDevious = (Math.random() * 100) > 90 ? true : false;
    var i;

    /*
     * First off, code is way more readable with smaller variable names.
     * Don't be afraid to store things with long names in variables
     */
    var votingPercents = window.GameVariables.AskTheAudienceVotingPercents;
    var currentAnswer = window.GameVariables.CurrentCorrectAnswer;

    var answerAIsOut = window.GameVariables.AnswerAIsOut;
    var answerBIsOut = window.GameVariables.AnswerBIsOut;
    var answerCIsOut = window.GameVariables.AnswerCIsOut;
    var answerDIsOut = window.GameVariables.AnswerDIsOut;

    /*
     * When you want to do the same thing to something multiple times, you 
     * want to use a loop
     * You might also want to look into using switch/case over if/else 
     * whenever it makes sense
     */
    var answers = [answerAIsOut, answerBIsOut, answerCIsOut, answerDIsOut];

    for (i = 0 ; i < answers.length ; i++) {
        if (answers[i]) {
            votingPercents[i] = 0;
        }
    }
    /*
     * So that things are simplified to numbers, why not put A, B, C, and D 
     * into an array?
     */

    var possibleAnswers = ["A", "B", "C", "D"];

    /* 
     * You don't need to compare anything to true, if (true) evaluates to true
     * so we can replace all those comparisons to condense the code a bit
     */

    if (!beDevious) {
        if (currentAnswer == possibleAnswers[0] && !answers[0]) {
            votingPercents[0] = Math.ceil(Math.random() * (100 - percentageOfDifficulty));
            votingPercents[1] = answers[1] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[0]));
            votingPercents[2] = answers[2] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[0] - votingPercents[1]));
            votingPercents[3] = answers[3] ? 0 : 100 - votingPercents[0] - votingPercents[1]  - votingPercents[2];
        }

        if (currentAnswer == possibleAnswers[1] && !answers[1]) {
            votingPercents[1] = Math.ceil(Math.random() * (100 - percentageOfDifficulty));
            votingPercents[2] = answers[2] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[1]));
            votingPercents[3] = answers[3] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[1] - votingPercents[2]));
            votingPercents[0] = answers[0] ? 0 : 100 - votingPercents[1] - votingPercents[2]  - votingPercents[3];
        }

        if (currentAnswer == possibleAnswers[2] && !answers[2]) {
            votingPercents[2] = Math.ceil(Math.random() * (100 - percentageOfDifficulty));
            votingPercents[3] = answers[3] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[2]));
            votingPercents[0] = answers[0] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[2] - votingPercents[3]));
            votingPercents[1] = answers[1] ? 0 : 100 - votingPercents[2] - votingPercents[3]  - votingPercents[0];
        }

        if (currentAnswer == possibleAnswers[3] && !answers[3]) {
            votingPercents[3] = Math.ceil(Math.random() * (100 - percentageOfDifficulty));
            votingPercents[0] = answers[0] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[3]));
            votingPercents[1] = answers[1] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[3] - votingPercents[0]));
            votingPercents[2] = answers[2] ? 0 : 100 - votingPercents[3] - votingPercents[0]  - votingPercents[1];
        }
    }
    else{
        if (!answers[0]) {
            votingPercents[0] = Math.ceil(Math.random() * (100));
            votingPercents[1] = answers[1] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[0]));
            votingPercents[2] = answers[2] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[0] - votingPercents[1]));
            votingPercents[3] = answers[3] ? 0 : 100 - votingPercents[0] - votingPercents[1]  - votingPercents[2];
        }

        if (!answers[1]) {
            votingPercents[1] = Math.ceil(Math.random() * (100));
            votingPercents[2] = answers[2] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[1]));
            votingPercents[3] = answers[3] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[1] - votingPercents[2]));
            votingPercents[0] = answers[0] ? 0 : 100 - votingPercents[1] - votingPercents[2]  - votingPercents[3];
        }

        if (!answers[2]) {
            votingPercents[2] = Math.ceil(Math.random() * (100));
            votingPercents[3] = answers[3] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[2]));
            votingPercents[0] = answers[0] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[2] - votingPercents[3]));
            votingPercents[1] = answers[1] ? 0 : 100 - votingPercents[2] - votingPercents[3]  - votingPercents[0];
        }

        if (!answers[3]) {
            votingPercents[3] = Math.ceil(Math.random() * (100));
            votingPercents[0] = answers[0] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[3]));
            votingPercents[1] = answers[1] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[3] - votingPercents[0]));
            votingPercents[2] = answers[2] ? 0 : 100 - votingPercents[3] - votingPercents[0]  - votingPercents[1];
        }
    }

    /*
     * Now let's do the same thing here as we did above and put this in a 
     * loop so we can easily add more items later if we need to
     */
    var sumOfAllPercents = 0;

    for (i = 0 ; i < votingPercents.length ; i++ ) {
        sumOfAllPercents += votingPercents[i];
    }

    if (sumOfAllPercents < 100) {
        for (i = 0 ; i < answers.length ; i++) {
            if (currentAnswer == possibleAnswers[i] && !answers[i]) {
                votingPercents[i] += 100 - sumOfAllPercents;
            }
        }
    }

    for (i = 0 ; i < votingPercents.length ; i++) {
        votingPercents[i] = votingPercents[i] / 100;
    }
}

好的,现在您的代码是可调试的,让我们开始寻找我们的算法:

function calculateGraphPercentages() {
    var percentageOfDifficulty = (window.GameVariables.QuestionLevel - 1) * 5;
    var beDevious = (Math.random() * 100) > 90 ? true : false;
    var i;


    /*
     * Instead of referencing global variables, why not pass all this info to calculateGraphPercentages like this:
     * calculateGraphPercentages(answers, votingPercents, currentAnswer)
     * or use some method to manage global state like Flux
     */
    var votingPercents = window.GameVariables.AskTheAudienceVotingPercents;
    // Save the number of votes so we can reuse it
    var numVotes = votingPercents.length

    var currentAnswer = window.GameVariables.CurrentCorrectAnswer;

    var answerAIsOut = window.GameVariables.AnswerAIsOut;
    var answerBIsOut = window.GameVariables.AnswerBIsOut;
    var answerCIsOut = window.GameVariables.AnswerCIsOut;
    var answerDIsOut = window.GameVariables.AnswerDIsOut;

    /*
     * When you want to do the same thing to something multiple times, you 
     * want to use a loop
     * You might also want to look into using switch/case over if/else 
     * whenever it makes sense
     */
    var answers = [answerAIsOut, answerBIsOut, answerCIsOut, answerDIsOut];

    for (i = 0 ; i < numVotes ; i++) {
        if (answers[i]) {
            votingPercents[i] = 0;
        }
    }
    /*
     * So that you simplify things to just numbers, why not put 
     * A, B, C, and D into an array?
     * You could also make this extensible by iterating over the alphabet 
     * (or whatever set of characters you choose) and stopping at numVotes
     */

    var possibleAnswers = ["A", "B", "C", "D"];

    /* 
     * Now you can try to find patterns in the numbers and figure out how to 
     * create a loop or recursive function to iterate through a progression 
     * of numbers so all this code is easier to read and not repeated.
     * DRY: Don't Repeat Yourself
     */

    /*
     * Let's start by putting common functionality into it's own function:
     */

     /*
            votingPercents[0] = Math.ceil(Math.random() * (100 - percentageOfDifficulty));
            votingPercents[1] = answers[1] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[0]));
            votingPercents[2] = answers[2] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[0] - votingPercents[1]));
            votingPercents[3] = answers[3] ? 0 : 100 - votingPercents[0] - votingPercents[1]  - votingPercents[2];
    */

    /*
     * We'll add a difficulty parameter so we can re-use this function in both places
     * We'll use the modulus operator to wrap around
     */
    function calcPercents(currentIndex, numIndexes, difficulty) {
        var votingPercents = [];
        var lastIndex = numIndexes + currentIndex;

        votingPercents[currentIndex] = Math.ceil(Math.random() * difficulty);
        var totalPercents = votingPercents[currentIndex];

        for (var i = currentIndex + 1; i <= lastIndex ; i++) {
            totalPercents += votingPercents[i % numIndexes]
            votingPercents[i % numIndexes] = answers[i % numIndexes] ? 0 : Math.ceil(Math.random() * (100 - totalPercents))
        }
        votingPercents[lastIndex] = answers[lastIndex] ? 0 : 100 - totalPercents;
    }

    /*
     * Now we can reuse that function easily
     */

    if (!beDevious) {
        for (i = 0 ; i < numVotes ; i++) {
            calcPercents(i, numVotes, 100 - percentageOfDifficulty)
        }
    } else {
        for (i = 0 ; i < numVotes ; i++) {
            calcPercents(i, numVotes, 100)
        }
    }

    /*
     * Now let's do the same thing here as we did above and put this in a 
     * loop so we can easily add more items later if we need to
     */
    var sumOfAllPercents = 0;

    for (i = 0 ; i < numVotes ; i++ ) {
        sumOfAllPercents += votingPercents[i];
    }

    if (sumOfAllPercents < 100) {
        for (i = 0 ; i < numVotes ; i++) {
            if (currentAnswer == possibleAnswers[i] && !answers[i]) {
                votingPercents[i] += 100 - sumOfAllPercents;
            }
        }
    }

    for (i = 0 ; i < numVotes ; i++) {
        votingPercents[i] = votingPercents[i] / 100;
    }
}

太好了,现在我们有了一个算法。让我们让整个东西可重用,看看我们得到了什么

// A function that calculates the voting percentages based on a difficulty rating
function calcPercents(currentIndex, numIndexes, difficulty) {
    var votingPercents = [];
    var lastIndex = numIndexes + currentIndex;

    votingPercents[currentIndex] = Math.ceil(Math.random() * difficulty);
    var totalPercents = votingPercents[currentIndex];

    for (var i = currentIndex + 1; i <= lastIndex ; i++) {
        totalPercents += votingPercents[i % numIndexes]
        votingPercents[i % numIndexes] = answers[i % numIndexes] ? 0 : Math.ceil(Math.random() * (100 - totalPercents))
    }
    votingPercents[lastIndex] = answers[lastIndex] ? 0 : 100 - totalPercents;

    return votingPercents;
}

// A function that calculates graph percentages
function calculateGraphPercentages(answers, votingPercents, currentAnswer) {
    var percentageOfDifficulty = (window.GameVariables.QuestionLevel - 1) * 5;
    var beDevious = (Math.random() * 100) > 90 ? true : false;
    var i;

    // Save the number of votes so we can reuse it
    var numVotes = votingPercents.length;
    var possibleAnswers = [];
    var sumOfAllPercents = 0;

    for (i = 0 ; i < numVotes ; i++) {
        if (answers[i]) {
            votingPercents[i] = 0;
        }
        // Create an alpha-numeric list of answers
        possibleAnswers.push(String.fromCharCode(65 + 1));
    }

    if (!beDevious) {
        for (i = 0 ; i < numVotes ; i++) {
            votingPercents[i] = calcPercents(i, numVotes, 100 - percentageOfDifficulty)
        }
    } else {
        for (i = 0 ; i < numVotes ; i++) {
            votingPercents[i] = calcPercents(i, numVotes, 100)
        }
    }

    for (i = 0 ; i < numVotes ; i++ ) {
        sumOfAllPercents += votingPercents[i];
    }

    if (sumOfAllPercents < 100) {
        for (i = 0 ; i < numVotes ; i++) {
            if (currentAnswer == possibleAnswers[i] && !answers[i]) {
                votingPercents[i] += 100 - sumOfAllPercents;
            }
        }
    }

    for (i = 0 ; i < numVotes ; i++) {
        votingPercents[i] = votingPercents[i] / 100;
    }
}

然后我会这样调用它:

var votingPercents = window.GameVariables.AskTheAudienceVotingPercents;
var currentAnswer = window.GameVariables.CurrentCorrectAnswer;

var answerAIsOut = window.GameVariables.AnswerAIsOut;
var answerBIsOut = window.GameVariables.AnswerBIsOut;
var answerCIsOut = window.GameVariables.AnswerCIsOut;
var answerDIsOut = window.GameVariables.AnswerDIsOut;

var answers = [answerAIsOut, answerBIsOut, answerCIsOut, answerDIsOut];

calculateGraphPercentages(answers, votingPercents, currentAnswer);

但我会弄清楚如何将这些值放入您首先传递给函数的结构中,并使它们在您的应用程序中保持一致。

这段代码的可重用性更高,也更容易调试。希望这有帮助:)

关于javascript - 我对 Java 脚本缺少什么,请帮助我?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53582285/

相关文章:

javascript - 按钮未运行 JavaScript

javascript - React Portals 如何在不同的子树中保存来自提供者的上下文?

javascript - 处理 Typescript 中的 Promise 捕获

php - 单击清除或隐藏具有相同类名的标签

JavaScript:任何人都可以让它工作或解释为什么它不起作用吗?

php - 如何使表单提交按钮也重定向到页面中的特定 div?

javascript - 如何使用for循环在javascript中做表单提交?

javascript - 为什么 angular.lowercase 方法使用 'bitwise-or 32' 将 'A' 转换为 'a' ?为什么不使用 '+ 32' ?

javascript - 我可以将回调函数作为变量异步传递多个级别吗?

javascript - 如何从替换的实现中调用类中的方法?