Javascript初学者失败

时间:2014-03-16 21:02:24

标签: javascript arrays

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

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

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

2 个答案:

答案 0 :(得分:0)

不确定您的问题是否与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;

答案 1 :(得分:0)

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

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

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