这个javascript在保持测验得分意味着什么?

时间:2016-01-20 06:13:56

标签: javascript html

我对此代码有疑问。它应该保持测验的分数。

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])
    }
}

5 个答案:

答案 0 :(得分:1)

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)将input元素的值推入value_list数组。

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

2)如果选中了输入,则添加点。函数Numbervalue_list[i]中的值转换为数字,然后将其添加到points.we传递已检查输入的值tag作为函数的参数。

这种情况下的输入是一个复选框,其中包含已检查的属性和值。

<input type="checkbox" name="vehicle" value="Bike"> 

答案 1 :(得分:1)

<强>线路1:

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

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

<强>线路2:

var value_list = [];

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

<强>行3:

var point = 0;

使用值point初始化变量0

第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

最诚挚的问候,
普里

答案 2 :(得分:1)

// 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)
    }
}

答案 3 :(得分:0)

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])
    }
}

答案 4 :(得分:0)

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
    }
}