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

incorporate loops and arrays into my code

本文关键字:我的 代码 合并 循环 数组      更新时间:2023-09-26

对于我在编程类中所做的一个项目,我正在制作一个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>
}

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

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.");
}