将循环和数组合并到我的代码中

时间:2015-03-02 04:32:11

标签: javascript arrays loops

对于我正在编程课程中进行的项目,我正在制作一个Trivia游戏。现在我有三个选择题,在每个问题之后我有四个按钮,可能的答案是用户可以点击的。如果他们得到正确的答案,那么问题旁边会出现一张支票。这就是我的所有计划。对于我的项目,我还需要包含至少一个循环和一个数组。我能添加的各种想法吗?到目前为止,这是我的代码:

<!DOCTYPE html>
<html>
<head>
    <title> Geography Trivia</title>
    <link rel="stylesheet" type="text/css" href="firstproject.css">
</head>
<body>
    <h1> Geography Trivia </h1>
    <h3> Questions: </h3>
        <p> where is the atacama desert located? </p>
            <button onclick="displayPromptOne()">Africa</button>
            <button onclick="displayPromptOne()">Canada</button>
            <button onclick="displayPromptOne()">Russia</button>
            <button onclick="showCheckOne()" >South America</button>
            <img id="checkOne" src="check.jpg" style="height: 45px; width: 45; opacity:0;">
        <p> Which Lake in Canada has the most volume when compared to all other lakes in the world </p>
            <button onclick="showCheckTwo()"> Raindeer Lake </button>
            <button onclick="displayPromptOne()"> Anderson Lake </button>
            <button onclick="displayPromptOne()"> Tagish Lake </button>
            <button onclick="displayPromptOne()"> Teslin Lake </button>
            <img id="checkTwo" src="check.jpg" style="height: 45px; width: 45; opacity:0;">
        <p> Which U.S. state is nicknamed "the sunshine state"?</p>
            <button onclick="displayPromptOne()"> California </button>
            <button onclick="displayPromptOne()"> Arizona </button>
            <button onclick="showCheckThree()"> Florida</button>
            <button onclick="displayPromptOne()">Pheonix </button>
            <img id="checkThree" src="check.jpg" style="height: 45px; width: 45; opacity:0;">



<script>
function showCheckOne() {
    document.getElementById('checkOne').style.opacity= '1'
}
function showCheckTwo() {
    document.getElementById('checkTwo').style.opacity= '1'
}
function showCheckThree() {
    document.getElementById('checkThree').style.opacity= '1'
}

function displayPromptOne() {
    alert("Sorry, wrong answer! Try Again");
}
</script>

</body>
</html>
}

2 个答案:

答案 0 :(得分:1)

你可以有一系列问题,你可以随机挑选一个固定的子集来询问用户。这将要求您同时使用数组和循环;并且似乎会在每次重新加载时提出随机/新问题。

答案 1 :(得分:0)

var myGradesThisYear = ['D-', 'F', 'D', 'D+', 'F'];

for (var index = 0; index < myGradesThisYear.length; index++) {
  alert("If I don't learn to do my own homework I'll never pass these classes.");
}