Variable 在某些函数中改变,在某些函数中不改变

问题描述 投票:0回答:0

我在这里真的很生气。这是我的代码:

var qi = 0;
var value = 0;
var correct = new Audio("correct.mp3");
var lifeline_waiting_sound = new Audio("life.mp3");
var loss = new Audio("loss.mp3");
var hasPass = true;
var fmsgtv;
var currentANS;
var questions = {
    "a": [
        {
            question: "What type of animals only eat meat?",
            val: 100,
            r_answer: 1,
            answers: [
            "Omnivore",
            "Carnivore",
            "Herbivore",
            "All of the above"
            ]
        },
        {
            question: "Which type of animal mainly gives live birth?",
            val: 200,
            r_answer: 0,
            answers: [
            "Mammals",
            "Reptiles",
            "Amphibians",
            "Birds"
            ]
        },
        {
            question: "These mammals lays eggs.",
            val: 300,
            r_answer: 2,
            answers: [
            "Humans",
            "Hippos",
            "Platypuses",
            "Hamsters"
            ]
        },
        {
            question: "Which of these terms mean warm-blooded?",
            val: 500,
            r_answer: 1,
            answers: [
            "Ectothermic",
            "Endothermic",
            "Both of the Above",
            "None of the Above"
            ]
        },
        {
            question: "Fish have a _ bladder.",
            val: 1000,
            r_answer: 3,
            answers: [
            "Lateral",
            "Afloat",
            "Cartilage",
            "Swim"
            ]
        },
        {
            question: "Which types of vertabrates have thin, moist skin?",
            val: 2000,
            r_answer: 2,
            answers: [
            "Reptiles",
            "Mammals",
            "Amphibians",
            "Birds"
            ]
        },
        {
            question: "What was the first group of fish to ever evolve?",
            val: 4000,
            r_answer: 2,
            answers: [
            "Bony",
            "Cartilaginous", 
            "Jawless",
            "All of the above"
            ]
        },
        {
            question: "Which of these animals have the longest gestation period?",
            val: 8000,
            r_answer: 2,
            answers: [
            "Dog",
            "Human",
            "Alpaca",
            "Lion"
            ]
        },
        {
            question: "What are nares?",
            val: 16000,
            r_answer: 0,
            answers: [
            "Nostrils",
            "A Part of the Lungs",
            "Little Bones in the Ear",
            "Toes"
            ]
        },
        {
            question: "There are over/about _____ species of fish in the world.",
            val: 32000,
            r_answer: 1,
            answers: [
            "99,999",
            "34000",
            "27000",
            "41000"
            ]
        },
        {
            question: "What are the 3 orders of amphibians?",
            val: 64000,
            r_answer: 3,
            answers: [
            "Anura, Prototheria, Osteichthyes",
            "Metatheria, Eutheria, Apoda",
            "Placentals, Squamata, Crocodylia",
            "Anura, Caudata, Apoda"
            ]
        },
        {
            question: "What habitats do reptiles NOT live in?",
            val: 125000,
            r_answer: 0,
            answers: [
            "Tundra, Polar Ice",
            "Polar Ice, Desert",
            "Seasonal Forest, Ocean",
            "None of the above (they live in all of them)"
            ]
        },
        {
            question: "Primitive Fish date back how far?",
            val: 250000,
            r_answer: 1,
            answers: [
            "475 Million Years",
            "550 Million Years",
            "750 Million Years",
            "300 Million Years"
            ]
        },
        {
            question: "What group of animals have a two chambered heart?",
            val: 500000,
            r_answer: 2,
            answers: [
            "Reptiles",
            "Amphibians",
            "Fish",
            "Mammals"
            ]
        },
    ],
    "d":[
        {
            question: `What kind of eggs are these? <br><img src='https://th.bing.com/th/id/R.76877a36d3ea95356ed01edb0757b4a3?rik=wZOPeN8W1ceKVA&riu=http%3a%2f%2fvoice.gardenbird.co.uk%2fwp-content%2fuploads%2f2017%2f03%2fBlackbird-Eggs.jpg&ehk=LtJAYsynWLJDm5qUul%2bVg7ShYJk243%2fhDJ2H0rxDWCU%3d&risl=&pid=ImgRaw&r=0'
            style='width: 8.3vw; position: absolute; border-radius: 30.3vw; left: 34.3vw'>`,
            val: 100,
            r_answer: 2,
            answers: [
            "Fish Eggs",
            "Amphibian Eggs",
            "Bird Eggs",
            "Mammal/Platypuses' Eggs"
            ]
        },
        {
            question: "Axolotys are _______________.",
            val: 200,
            r_answer: 2,
            answers: [
            "Mammals",
            "Fish",
            "Amphibians",
            "Birds"
            ]
        },
        {
            question: "Birds have hollow bones because _____________________________.",
            val: 300,
            r_answer: 1,
            answers: [
            "they want to be cool",
            "it helps them fly",
            "it makes their transition into a bird to a tree faster",
            "it helps them walk on land"
            ]
        },
        {
            question: "Birds have _______________.",
            val: 500,
            r_answer: 0,
            answers: [
            "no teeth",
            "3 sets of teeth",
            "sharp teeth",
            "dull teeth"
            ]
        },
        {
            question: "Bison can fly.",
            val: 1000,
            r_answer: 2,
            answers: [
            "That statement is true.",
            "There is a minority that doesn't.",
            "That statement is false.",
            "Scientists don't know yet"
            ]
        },
        {
            question: "All variables have ________.",
            val: 2000,
            r_answer: 3,
            answers: [
            "a backbone",
            "no backbone",
            "the ability to speak",
            "None Of The Above"
            ]
        },
        {
            question: "What is gestation?",
            val: 4000,
            r_answer: 3,
            answers: [
            "<span style='font-size: 1.63vw;'>the period in which an animal is drastically changing/evolving</span>",
            "<span style='font-size: 1.63vw;'>the classification of a vertebrate based on it's habitat</span>", 
            "<span style='font-size: 1.63vw;'>the period in which a species population is rapidly declining</span>",
            "<span style='font-size: 1.63vw;'>the period from conception to the birth of an organism</span>"
            ]
        },
        {
            question: "Which of these animals have the longest gestation period?",
            val: 8000,
            r_answer: 2,
            answers: [
            "Dog",
            "Human",
            "Alpaca",
            "Lion"
            ]
        },
        {
            question: "Vertebrata is a ___________________.",
            val: 16000,
            r_answer: 1,
            answers: [
            "Domain",
            "Sub Phylum",
            "Sub Species",
            "Genus"
            ]
        },
        {
            question: "What is the scientific name for a bird?",
            val: 32000,
            r_answer: 0,
            answers: [
            "Aves",
            "Angularasaurus",
            "Birdauta",
            "Ctheodta"
            ]
        },
        {
            question: "Lizards are ____________.",
            val: 64000,
            r_answer: 2,
            answers: [
            "Mammals",
            "Amphibians",
            "Reptiles",
            "Not A Vertebrate"
            ]
        },
        {
            question: "Which of these animals do exist?",
            val: 125000,
            r_answer: 0,
            answers: [
            "Transparent Metallic Bird",
            "Flying Squirrel",
            "Desert Dolphin",
            "Human-Frog Snake Axolotl"
            ]
        },
        {
            question: "Amphibians are __________.",
            val: 250000,
            r_answer: 1,
            answers: [
            "Omnivore",
            "Carnivore",
            "Herbivore",
            "All of the above"
            ]
        },
        {
            question: "What is cutaneous breathing?",
            val: 500000,
            r_answer: 0,
            answers: [
            "<span style='font-size: 1.63vw;'>the ability to breath through skin</span>",
            "<span style='font-size: 1.63vw;'>the ability to breath both underwater and on land</span>",
            "<span style='font-size: 1.63vw;'>the ability to prevent breathing for up to 9 days</span>",
            "<span style='font-size: 1.63vw;'>the ability to safely inhale carbon-dioxide</span>"
            ]
        },
        {
            question: "<span style='color: gold;'>MILLION DOLLAR QUESTION</span><br>Which of these are NOT an actual phylum of fish?",
            val: 1000000,
            r_answer: 3,
            answers: [
            "Perciformes",
            "Sphymidae",
            "Siluriformes",
            "Phantaciuformes"
            ]
        },
    ]
}

var findAnswer = {
    r: function(){
        console.log(qi);
        return questions[localStorage.getItem("current_team")][qi].r_answer;
    },
    randomA:function(){
        /*var ranD = Math.floor(Math.random * 2) + 1;
        ranD2 = Math.floor(Math.random * 3);
        var final;
        if (ranD==1) {
           return  questions[localStorage.getItem("current_team")][qi].r_answer
        } else {
            while(true){
                if(!(ranD2==questions[localStorage.getItem("current_team")][qi].r_answer)){
                    final=questions[localStorage.getItem("current_team")][qi].answers[ranD2];
                    console.log("wrong");
                    return questions[localStorage.getItem("current_team")][qi].answers[ranD2];
                } else {
                    ranD2=Math.floor(Math.random * 3)
                    console.log("right")
                }
            } 
        }*/
    },
}

window.addEventListener("load", function(){
    localStorage.setItem("qi", qi)
    document.querySelector("[question]").innerHTML = questions[localStorage.getItem("current_team")][qi].question;
    document.querySelector("[ans='a']").innerHTML = "A. &#8194;" + questions[localStorage.getItem("current_team")][qi].answers[0];
    document.querySelector("[ans='b']").innerHTML = "B. &#8194;" + questions[localStorage.getItem("current_team")][qi].answers[1];
    document.querySelector("[ans='c']").innerHTML = "C. &#8194;" + questions[localStorage.getItem("current_team")][qi].answers[2];
    document.querySelector("[ans='d']").innerHTML = "D. &#8194;" + questions[localStorage.getItem("current_team")][qi].answers[3];
    for(let i = 0;i < document.querySelectorAll("[value]").length; i++){
        if(document.querySelectorAll("[value]")[i].getAttribute("value")==questions[localStorage.getItem("current_team")][qi].val){
            document.querySelectorAll("[value]")[i].style.color = "red";
        }
    }
})

function checkAnswer(elem){
    document.querySelector("[coi]").innerHTML = "----";
    document.querySelector("[coi]").style.color = "white";
    if(an.n==questions[localStorage.getItem("current_team")][qi].r_answer){
        document.querySelector("[valdec]").innerHTML = "";
        document.querySelector("[c_ans]").style.display = "block";
        correct.volume = 0.1;
        setTimeout(function(){
            correct.play()
        }, 1500)
        setTimeout(function(){
            document.querySelector("[coi]").innerHTML = "correct!";
            document.querySelector("[coi]").style.color = "green";
            value=questions[localStorage.getItem("current_team")][qi].val;
            document.querySelector("[valdec]").innerHTML = "Your team now has $" + value + ".";
            document.querySelector("[money_amount]").innerHTML = "$" + value;
            for(let i = 0;i < document.querySelectorAll("[value]").length; i++){
                if(document.querySelectorAll("[value]")[i].getAttribute("value")==questions[localStorage.getItem("current_team")][qi].val){
                    document.querySelectorAll("[value]")[i].style.color = "black";
                    document.querySelectorAll("[value]")[i].style.backgroundImage = "linear-gradient(grey, black)";
                }
            }
        }, 2000)
    } else {
        document.querySelector("[valdec]").innerHTML = "";
        document.querySelector("[c_ans]").style.display = "block";
        correct.volume = 0.1;
        setTimeout(function(){
            loss.play()
        }, 1000)
        setTimeout(function(){
            document.querySelector("[coi]").innerHTML = "incorrect!";
            document.querySelector("[coi]").style.color = "red";
            value=questions[localStorage.getItem("current_team")][qi].val;
            if(hasPass){
                document.querySelector("[valdec]").innerHTML = "Sorry! You still have a pass. Do you want to use it?";
                setTimeout(function(){
                    hasPass = false;
                }, 100)
            } else {
                document.querySelector("[valdec]").innerHTML = "You're team has no more passes, so unfortunately, that's it.";
            }
            for(let i = 0;i < document.querySelectorAll("[value]").length; i++){
                if(document.querySelectorAll("[value]")[i].getAttribute("value")==questions[localStorage.getItem("current_team")][qi].val){
                    document.querySelectorAll("[value]")[i].style.color = "black";
                    document.querySelectorAll("[value]")[i].style.backgroundImage = "linear-gradient(grey, black)";
                }
            }
        }, 3000)
    }
}
function newQuestion(){
    qi++
    localStorage.setItem("qi", qi)
    document.querySelector("[c_ans]").style.display = "none";
    document.querySelector("[question]").innerHTML = questions[localStorage.getItem("current_team")][qi].question;
    document.querySelector("[ans='a']").innerHTML = "A. &#8194;" + questions[localStorage.getItem("current_team")][qi].answers[0];
    document.querySelector("[ans='b']").innerHTML = "B. &#8194;" + questions[localStorage.getItem("current_team")][qi].answers[1];
    document.querySelector("[ans='c']").innerHTML = "C. &#8194;" + questions[localStorage.getItem("current_team")][qi].answers[2];
    document.querySelector("[ans='d']").innerHTML = "D. &#8194;" + questions[localStorage.getItem("current_team")][qi].answers[3];
    document.querySelector("[ans='a']").style.backgroundColor = "rgba(69, 69, 148, 0.61)";
    document.querySelector("[ans='b']").style.backgroundColor = "rgba(69, 69, 148, 0.61)";
    document.querySelector("[ans='c']").style.backgroundColor = "rgba(69, 69, 148, 0.61)";
    document.querySelector("[ans='d']").style.backgroundColor = "rgba(69, 69, 148, 0.61)";
    for(let i = 0;i < document.querySelectorAll("[value]").length; i++){
        if(document.querySelectorAll("[value]")[i].getAttribute("value")==questions[localStorage.getItem("current_team")][qi].val){
            document.querySelectorAll("[value]")[i].style.color = "red";
        }
    }
}

function opL(){
    document.querySelector("[cll]").style.display = "block";
    document.querySelector("[lhdtual]").innerHTML = document.querySelector("[inp]").value + " Has Decided To Use A Lifeline";
    lifeline_waiting_sound.currentTime = 0;
    lifeline_waiting_sound.play()
}

function startMSG(index){
    lifeline_waiting_sound.pause()
    document.querySelector("[msghldr]").innerHTML = "";
    document.querySelector("[msgs]").style.display = "block";
    document.querySelector("[msginpt]").style.display = "none";
    document.querySelector("[msginpt]").value=""
    var msg = document.createElement("div");
    msg.setAttribute("msg", "0");
    msg.innerHTML = ".";
    document.querySelector("[msghldr]").appendChild(msg);
    setTimeout(function(){
        msg.innerHTML = "..";
        document.querySelector("[msgdn]").innerHTML = lifelines[localStorage.getItem("current_team")][localStorage.getItem("sel_lfln")].tname;
    }, 500)
    setTimeout(function(){
        msg.innerHTML = "...";
    }, 1000)
    setTimeout(function(){
        pname = "Personsada";
        msg.innerHTML = "....";
    }, 1500)
    setTimeout(function(){
        msg.innerHTML = lifelines[localStorage.getItem("current_team")][localStorage.getItem("sel_lfln")].intmes;
    }, 2000)
    setTimeout(function(){
        document.querySelector("[msginpt]").style.display = "block";
    }, 7000)
    document.querySelector("[questMSGBX]").innerHTML = questions[localStorage.getItem("current_team")][qi].question;
    document.querySelector("[anslist]").innerHTML = `
        <li>${questions[localStorage.getItem("current_team")][qi].answers[0]}</li>
        <li>${questions[localStorage.getItem("current_team")][qi].answers[1]}</li>
        <li>${questions[localStorage.getItem("current_team")][qi].answers[2]}</li>
        <li>${questions[localStorage.getItem("current_team")][qi].answers[3]}</li>
    `;
}

window.addEventListener("load", function(){
    //startMSG()
})

function submitMessageTwo(){
    var msg2 = document.createElement("div");
    msg2.setAttribute("msg", "1");
    msg2.innerHTML = document.querySelector("[msginpt]").value;
    msg2.style.top = ((parseInt(lifelines[localStorage.getItem("current_team")][localStorage.getItem("sel_lfln")].intmes.split("").length)/31)*1.83+4.3).toString() + "vw";
    fmsgtv = ((parseInt(lifelines[localStorage.getItem("current_team")][localStorage.getItem("sel_lfln")].intmes.split("").length)/31)*1.83+4.3);
    document.querySelector("[msghldr]").appendChild(msg2);
    setTimeout(function(){
        responseMSG()
    }, 2000)
}

function responseMSG(){
    var msg12 = document.createElement("div");
    msg12.setAttribute("msg", "0");
    msg12.innerHTML = "";
    msg12.style.top = (fmsgtv+18.3).toString() + "vw";
    msg12.style.position = "fixed";
    document.querySelector("[msghldr]").appendChild(msg12);
    msg12.style.width = "9.3vw";
    setTimeout(function(){
        msg12.style.width = "26.3vw";
    }, 200)
    setTimeout(function(){
        msg12.innerHTML = lifelines[localStorage.getItem("current_team")][localStorage.getItem("sel_lfln")].respnmes;
    }, 1500)
    console.log(qi + "this")
}

所以在 HTML 文件中有一个按钮,当你按下它时,变量 qi 上升。然后,在另一个 JS 文件中,当被命令时,文本被放置在包括使用 findAnswer.r() 的文档上;但是无论何时返回它返回的任何内容,它都会将 qi 视为它的值仍然为 0。任何人都可以在我的代码中找到任何错误吗? Visual Studio Code 没有给我任何错误,也没有给我文档上的控制台,所以我很困惑。

我尝试使用 setInterval 不断检查 qi 的值,每次单击按钮时它都会上升,但是当文本设置为使用 findAnswer.r() 显示在屏幕上时,我将其设置为 console.log( ) qi是什么,它说0; 我也尝试制作一个数组,我做了一个间隔,它不断地将数组更改为

----[qi].answers 
。然后我让它也记录数组,它说数组是空的。就像我说的,所有其他使用气的东西都很好,比如
checkAnswer()
。它也是同一个文档中的同一个变量。

javascript variables setinterval
© www.soinside.com 2019 - 2024. All rights reserved.