Javascript初学者失败



我目前正在为一个类的任务,我需要构建数组的数组来生成一个程序,允许用户输入他/她的名字和出生日期。我的代码如下,

// global array
var globalNameArray = [];
//setting up variablrd
//DOB Conversion
var getAge = function (dateString) {
    var today = new Date();
    var birthDate = new Date(dateString);
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
        age--;
    };
    return age;
};
var initialize = function () {
    "use strict";
// list of all objects being used in functions
    var paraRef, name, DOB, sum, mean, count,age, meanRef, sumRef, countRef, nameAndDOB, tempArray = [], i;
    //connecting the elements to the html page
    paraRef = document.getElementById('displayNumbers');
    name = document.getElementById('name').value;
    DOB = document.getElementById('DOB').value;
    meanRef = document.getElementById('mean');
    sumRef = document.getElementById('sum');
    countRef = document.getElementById('count');
    age = getAge(DOB);
    //arrays - name and DOB Array
    nameAndDOB = [];
// array with what information going into it
    nameAndDOB = [ name, DOB];
// pushing the names array into the global array
    globalNameArray.push(nameAndDOB);
    count = globalNameArray.length;
    countRef.innerHTML = count;
// for loop for temp array getting the inputs and adding it to the global
    /*sum = 0;
    for (i = 0; i < count; i += 1) {
        tempArray = [];
        tempArray = globalNameArray[i];
        sum += (tempArray[1]);
    }*/
    paraRef.innerHTML += name;
    paraRef.innerHTML += "  ";
    paraRef.innerHTML += DOB;
    paraRef.innerHTML += "<br>";
    //showing the values
    sumRef.innerHTML = sum;
    meanRef.innerHTML = mean;
};

我似乎不能显示用户的转换年龄,也不能显示所有年龄的平均值和总和,有人知道我能做什么吗?

不确定您的问题是否与计算或在HTML中插入值有关,但您的循环是一个良好的开始:

nameAndDOB = {"name":name, "DOB": DOB}; // i use an object but an array is fine too.
globalNameArray.push(nameAndDOB);
var sum = 0;
for (i = 0; i < count; i++) {    
    sum += getAge(globalNameArray[i].DOB);
}
var mean = (sum / count).toFixed(2);
//showing the values
sumRef.innerHTML = sum;
meanRef.innerHTML = mean;

简述如下,编写程序输入一系列姓名和相关的出生日期(yyy-mm-dd),并将它们放入数组的数组中。姓名、出生日期和年龄可以每行显示一个,后面是计数、总年龄和平均年龄。

用户一次输入一个名称和关联的DoB。姓名和出生日期存储在数组的数组中。可以显示所有姓名、出生日期和年龄的列表,每行显示一个,后面是计数、年龄总和和平均年龄。注意:alert命令不能用于显示数据,prompt命令不能用于输入数据。{html代码)

<!DOCTYPE html>
<html>
<head>
<title> PSP01</title>
<script type="text/javascript" src="psp1.js">
</script>
</head>
<body>
<h2>PSP01 Assignment</h2>
<p>
Enter a name and a DOB to add to the array
</p>
<form  action="#">
<table border="1">
<tr>
<td style = "text-align:right;">Name:</td>
<td>
   <input id ="name" type="text" />
</td>
<td rowspan="2">
   <input type="button" id="submit"  value="submit" />
</td>
</tr>
<tr>
<td style = "text-align:right;">DOB:</td>
<td>
   <input id="DOB" type="date" />
</td>
</tr>
<br />
<br>
</br>
<table border="1">
<tr>
<td style = "text-align:right;">Age:</td>
<td id="Age" style="width:100px">&#160;</td>
<td style = "text-align:right;">count:</td>
<td id="count" style="width:100px">&#160;</td>
<td style = "text-align:right;">Sum:</td>
<td id="sum" style="width:100px">&#160;</td>
<td style="text-align:right;">Mean:</td>
<td id="mean" style="width:100px">&#160;</td>
</tr>
</table>
</form>
<p id="displayNumbers"> </p> 
<script>
//add an event listener to the submit button
document.getElementById("submit").addEventListener('click', initialize, false);
</script>
</body>
</html>

最新更新