这个关于保持测验分数的 JavaScript 是什么意思

what does this javascript on keeping score for quiz mean?

本文关键字:JavaScript 是什么 意思 于保持      更新时间:2023-09-26

我有一个关于这段代码的问题。它应该保持测验的分数。

function point() {
var questions = document.getElementsByTagName("input");
var value_list = [];
var point = 0;
for (var i = 0; i < questions.length; i++) {
    value_list.push(questions[i].value);
    if (questions[i].checked) {
        point += Number(value_list[i])
    }
}
var questions = document.getElementsByTagName("input");

获取具有标记<input>的所有元素。questions是所有这些元素的数组。

var value_list = [];
var point = 0;

初始化数组和变量。

for (var i = 0; i < questions.length; i++) {

对于 questions 数组中的所有输入元素,请执行以下操作。

value_list.push(questions[i].value);

1) 将输入元素的值推入 value_list 数组。

if (questions[i].checked) {
    point += Number(value_list[i])
}

2) 如果检查输入,则添加点。函数Number会将 value_list[i] 中的值转换为数字,然后将其添加 points。我们将选中的输入标签的值作为参数传递给函数。

在这种情况下,输入是一个复选框,其中选中了属性和值。

<input type="checkbox" name="vehicle" value="Bike"> 
// We define a new function, named point. In this case, it doesn't receive any parameters.
function point() {
    // We get all the input elements, and we store it in the variable 'questions' 
    var questions = document.getElementsByTagName("input");
    // We are creating a new empty list
    var value_list = [];
    // We declare a variable initialized at 0
    var point = 0;
    // we are going to loop over the items inside questions (all the input elements)
    for (var i = 0; i < questions.length; i++) {
        // We get the value of the questions of the current iteration (questions[i].value) and we store it inside the empty list
        value_list.push(questions[i].value);
        // If the input is checked...
        if (questions[i].checked) {
            // we increment the value of point with the value in value_list[i], in this case it should be the same as questions[i],
            // because previously we store questions[i] inside value_list 
            point += Number(value_list[i])
    }
}

你只是遍历所有输入,如果输入被选中,你用它的值递增point变量。可以在此代码中简化

// We define a new function, named point. In this case, it doesn't receive any parameters.
function point() {
    // We get all the input elements, and we store it in the variable 'questions' 
    var questions = document.getElementsByTagName("input");
    // We declare a variable initialized at 0
    var point = 0;
    // we are going to loop over the items inside questions (all the input elements)
    for (var i = 0; i < questions.length; i++) {
        if (questions[i].checked) {
            point += Number(questions[i].value)
    }
}

第 1 行:

var questions = document.getElementsByTagName("input");

HTML 文件中有一些带有标记输入的元素。因此,您正在使用document.getElementsByTagName("input")将这些元素数组分配给变量问题。因此,这一行的结果是您将获得一个变量问题,它将包含HTML文档的所有输入元素

第 2 行:

var value_list = [];

此行用于定义数组变量value_list并为其分配空数组。

第 3 行:

var point = 0;

使用值 0 初始化变量point

4号线至终点:

// for (var i = 0; i < questions.length; i++) =>for loop   syntax will iterate till the length of questions(Which is array of input elements")

假设您有两个输入标签元素,它将迭代 2 次。

//  value_list.push(questions[i].value);=>taking value of ith input    element and pushing to "**value_list**" array variable
//  if (questions[i].checked) { =>  
// { checking if ith element is checked}
      point += Number(value_list[i])=>
//{ converting to munber and adding to point variable.
//  }
//}

此代码的结果将是输入标记具有的所有值的总和。假设您有一个如下所示的 HTML 文件:

<html><input type="checkbox" value="10">
  <input type="checkbox" value="20">
</html>

请注意,只有复选框的输入类型才选中属性。此代码成功执行后,将0 + 10 + 20点。点将保持 value = 30

此致敬意
普里扬卡

function point() {
    var questions = document.getElementsByTagName("input"); 
    //This line gets all the input tags on your page. Instead you can give all your questions a common class and do this to get count
    document.getElementsByClass("className");  
    var value_list = []; //creates an empty array value_list.
    var point = 0; //intializes a variable called point = 0 for keeping the count.
    for (var i = 0; i < questions.length; i++) { 
    //Runs a for loop starting from 0 to number of questions on your screen less 1. So if you have 5 input tags this loop will run from 0 to 4.
    value_list.push(questions[i].value); //pushes the values of the questions into the array value_list.
    if (questions[i].checked) { //If questions have checked property then the point variable is trying to store a number.
        point += Number(value_list[i])
    }
}
function point() {
var questions = document.getElementsByTagName("input");

getElementsByTagName() 方法访问具有指定标记名的所有元素。

var value_list = [];

上行创建名称为 value_list 的空列表。

var point = 0;

设置变量点=0;此变量的目的是存储最终结果。

for (var i = 0; i < questions.length; i++) {
    #var i = 0 => sets a variable before the loop starts (var i = 0).
    #i < questions.length => defines the condition for the loop to run (i must be less than question.length). questions.length calculate total number of question.
    #i++ => increases a value (i++) each time the code block in the loop has been executed.
value_list.push(questions[i].value);

push() 方法将新项添加到数组的末尾。

    if (questions[i].checked) {
        point += Number(value_list[i])
        #Here point = point+ Number(value_list[i]), it means adds the new score
    }
}