JS分数判断

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <form action="" method="post">
            <input type="text" id="fraction" placeholder="请输入分数"/>
            <input type="button" value="请求奖励" οnclick="fr()"/>
        </form>
        <div id="dp"></div>
        
        <script type="text/javascript">
            function fr(){
            let fraction=document.getElementById('fraction').value;
            console.log(fraction);
            fraction = parseInt(fraction);
            if(fraction <0 || fraction>100) {
                
                fraction=document.getElementById('dp').innerHTML='请输入1-100之间的数';
                fraction = parseInt(fraction);
                return;
            }
            let msg='';
            if(fraction <=100 && fraction>=90){
                msg='奖励两个雪糕';
            }else if(fraction>=80){
                msg='奖励一个雪糕';
            }else if(fraction>=70){
                msg='奖励女子单打';
            }else if(fraction>=60){
                msg='奖励男子单打';
            }else if(fraction>=0){
                msg='奖励混合双打';
            }else if(escape(fraction).indexOf( "%u" )<0){
                msg= "包含字符";
            }
            document.getElementById('dp').innerHTML=msg;
            }
        </script>
    </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的JS选择题题目,同时编写代码来判断用户的答案并统计分数。题目如下: ``` 1. What does CSS stand for? a) Creative Style Sheets b) Computer Style Sheets c) Cascading Style Sheets d) Colorful Style Sheets 2. What is the correct syntax for referring to an external script called "script.js"? a) <script href="script.js"> b) <script name="script.js"> c) <script src="script.js"> 3. How do you declare a JavaScript variable? a) var carName; b) variable carName; c) v carName; 4. What is the correct way to write a JavaScript array? a) var colors = "red", "green", "blue"; b) var colors = ["red", "green", "blue"]; c) var colors = {"red", "green", "blue"}; 5. Which operator is used to assign a value to a variable? a) = b) == c) === d) != 6. How do you write "Hello World" in an alert box? a) alertBox("Hello World"); b) msgBox("Hello World"); c) alert("Hello World"); d) msg("Hello World"); ``` 现在,为了判断用户的答案并统计分数,我们可以编写以下代码: ```javascript // 问题和答案 const questions = [ { question: "1. What does CSS stand for?", answer: "c" }, { question: "2. What is the correct syntax for referring to an external script called 'script.js'?", answer: "c" }, { question: "3. How do you declare a JavaScript variable?", answer: "a" }, { question: "4. What is the correct way to write a JavaScript array?", answer: "b" }, { question: "5. Which operator is used to assign a value to a variable?", answer: "a" }, { question: "6. How do you write 'Hello World' in an alert box?", answer: "c" } ]; // 分数 let score = 0; // 为每个问题创建一个单选框,并将其添加到HTML页面中 const quizContainer = document.getElementById("quiz"); const resultsContainer = document.getElementById("results"); const submitButton = document.getElementById("submit"); function buildQuiz() { const output = []; questions.forEach((currentQuestion, questionNumber) => { const answers = []; for (letter in currentQuestion) { if (letter !== "question") { answers.push( `<label> <input type="radio" name="question${questionNumber}" value="${letter}"> ${letter}: ${currentQuestion[letter]} </label>` ); } } output.push( `<div class="question"> ${currentQuestion.question} </div> <div class="answers"> ${answers.join("")} </div>` ); }); quizContainer.innerHTML = output.join(""); } // 计算分数 function showResults() { const answerContainers = quizContainer.querySelectorAll(".answers"); questions.forEach((currentQuestion, questionNumber) => { const answerContainer = answerContainers[questionNumber]; const selector = `input[name=question${questionNumber}]:checked`; const userAnswer = (answerContainer.querySelector(selector) || {}).value; if (userAnswer === currentQuestion.answer) { score++; answerContainers[questionNumber].style.color = "lightgreen"; } else { answerContainers[questionNumber].style.color = "red"; } }); resultsContainer.innerHTML = `You scored ${score} out of ${questions.length}!`; } // 在提交按钮被单击时,计算分数 submitButton.addEventListener("click", showResults); // 构建问题 buildQuiz(); ``` 这段代码将创建一个包含每个问题和答案的数组。然后,它将为每个问题创建一个单选框,并将其添加到HTML页面中。最后,它将在用户单击提交按钮时计算用户的得分并显示结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值