JS
var score = 0
var yes = "yes"
var pokemonName = [];
var bg = [];
var index = 0;
document.getElementById('repete').style.visibility = 'hidden';
(function asyncLoop() {
background = bg[num = Math.floor(Math.random() * bg.length)];
document.body.style.backgroundImage = 'url(' background ')';
})();
function loop() {
var myAnswer = document.getElementById("myAnswer");
var answer = myAnswer.value;
if (answer.toLowerCase().trim() == pokemonName[num].toLowerCase().trim()) {
document.getElementById("text").innerHTML = "Correct, do you want to try again?";
score
document.getElementById('repete').style.visibility = 'visible';
} else {
document.getElementById("text").innerHTML = "Incorrect, do you want to try again?" "\n" " The pokemon was " pokemonName[num];
document.getElementById('repete').style.visibility = 'visible';
}
}
function loopRepete() {
var repete1 = document.getElementById("repete");
var replay = repete1.value;
if (replay.toLowerCase().trim() == yes.toLowerCase().trim()) {
asyncLoop();
} else {
document.getElementById("text").innerHTML = "Goodbye, your score is " score;
location.href = 'index.html'
}
}
HTML
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Pokemon Quiz</title>
</head>
<body>
<lable> Answer </lable>
<input id="myAnswer" type="text">
<button onclick="loop()">click</button>
<p id="text"></p>
<div id="repete">
<lable> Answer </lable>
<input id="loop" type="text">
<button onclick="loopRepete()">click</button>
<p id="loopText"></p>
</div>
<script src="Gen3.js">
</script>
</body>
</html>
當我嘗試從第二個按鈕(div id = repete)獲取輸入并在其上放置一個 toLowerCase() 時它不起作用,我們洗掉了 toLowerCase() 它仍然不起作用但不顯示頁面上的控制臺錯誤。所以我對我需要嘗試做什么感到困惑。我試圖用谷歌搜索它,但我找不到任何有幫助的東西。
uj5u.com熱心網友回復:
您必須檢查陣列中是否num存在pokemonName。否則,你會做這樣的事情:
[1,2,3][5].toLowerCase()結果是undefined.toLowerCase(),你不能呼叫.toLowerCase(),undefined因為它只存在于String。
uj5u.com熱心網友回復:
如果你想定義 asyncLoop,你不能把它包裝在這樣的 IIFE 中。我懷疑您這樣做是為了在頁面加載時運行,但是還有另一種方法可以做到這一點并且仍然定義了供以后使用的函式:
// define it like a normal function
function asyncLoop() {
background = bg[num = Math.floor(Math.random() * bg.length)];
document.body.style.backgroundImage = 'url(' background ')';
}
// and call it right away
asyncLoop();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/340828.html
標籤:javascript html
