如何使用Javascript计算正确答案的数量? [英] How to count the number of correct answers using Javascript?

查看:51
本文介绍了如何使用Javascript计算正确答案的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的网站,以计算我举起的手指的数量(随机数).但是,我想在网站上添加一些其他内容.我正在尝试在容器的侧面创建一个计数器,用于计算正确答案的数量,并且每当用户错误地收到问题时,该计数器就会降为零.这是我的网站代码...

I'm trying to create a simple website about counting the number of fingers I'm putting up (random number). However, I want to add something extra to the website. I'm trying to create a counter on the side of the container that counts the number of correct answers, and drops down to zero whenever the user get a question incorrectly. Here is my code for the site...

    <!doctype html>
<html>
    <head>  
        <title>Changing Website Content</title>
        <link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
        <style type="text/css">
            body { 
               background-image: url(nyc.jpg);
               width:100%;
               font-family: 'Open Sans', sans-serif;    
            }
            #main {
                margin: 100px;
                background-color: #ede1e1;
                padding: 50px;
                position: relative;
                top: 50px;
            }
        </style>
    </head>
    <body>
        <div id="main">
            <p>How many fingers am I holding up? Pick a number 1 - 5.</p>
            <input id="fingers" type="text">
            <button type="submit" id="fingerChecker">Guess?</button>
        </div>
        <script type="text/javascript">
           document.getElementById("fingerChecker").onclick = function() {
                var fingers = document.getElementById("fingers").value;
                var random1 = (Math.floor((Math.random() * 5) + 1))
                if (fingers == (random1)) {
                    alert("Yes, you are correct!")
                } else {
                    alert("Nope! I had " + random1);
                }
           }
        </script>
    </body>
</html>

欢迎任何帮助.(这是我的第一篇文章,所以请原谅我可能犯的任何错误!)

Any help is welcomed. (This is my fist post so excuse any mistakes I may have made!)

推荐答案

您应该创建一个得分变量,如果答案正确,则增加得分并将其设置为得分元素的 innerHTML .如果用户输入的答案有误,请将分数重置为0.

You should create a score variable and if the answer is correct increase the score and set it to the innerHTML of a score element. If the user gets an incorrect answer reset the score to 0.

var score = 0;
document.getElementById("fingerChecker").onclick = function() {

  var fingers = document.getElementById("fingers").value;
  var random1 = (Math.floor((Math.random() * 2) + 1))

  if (fingers == (random1)) {

    alert("Yes, you are correct!");
    score++;
    
  } else {
    alert("Nope! I had " + random1);
    score = 0;
  }
  
 document.getElementById("score").innerHTML = score;
}

<div id="main">

  <p>How many fingers am I holding up? Pick a number 1 - 5.</p>
  <input id="fingers" type="text">
  <button type="submit" id="fingerChecker">Guess?</button>

</div>
<div id="score">0</div>

这篇关于如何使用Javascript计算正确答案的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆