var people = new Array();
function People (name, location, age){
this.name = name;
this.location = location;
this.age = age;
}
人を生成してテーブルにロードするための関数が他に 2 つあります。
function generatePeople(){}
function loadPeopleIntoTable(){}
基本的には、人のリストを調べて名前を取得し、そのテーブルに表示される最も一般的なファースト ネームを表示する必要があります。その関数は単に commonFirstName() と呼ばれます。
function commonFirstName(){}
与えられたヒントは「JavaScript オブジェクトは文字列でインデックス付けできる」ですが、それが 100% 理解できません。配列を調べて一般的な名前を見つけるためのコードは既に作成しています。しかし、そのリストを調べるために peoples 配列を呼び出すことはできません。commonFirstName() で手動で作成された配列でのみ動作させることができます。何故ですか?
必要に応じて、さらに説明を提供できます。
function commonFirstName(){
alert(people[1]);
//Rest of code that does the occurrences/name here
}
この出力は単に [object Object] です。
一方で:
function commonFirstName(){
tempArray = ['John Smith', 'Jane Smith', 'John Black'];
//Run through algorithm for finding common name.
}
「Common Name: John. Occurs 2 times」の警告出力を与える
次のような関数を介して配列 people を渡すだけだと思っていました。
function commonFirstName(people){
alert(people[1]);
}
私に何かを与える必要があります。この時点では名前だけではなく、少なくとも要素 1 のフルネーム、場所、年齢、またはそれらのいずれかを期待しています。配列が存在しないか、単に空であるかのように、まったく実行されません。
これは私が持っているすべてのコードです:
var PEOPLECOUNT = 100;
var people = new Array();
function People(name, location, age) {
this.name = name;
this.location = location;
this.age = age;
}
function initPage() {
generateTableRows();
generatePeople();
}
function generateTableRows() {
var table = document.getElementById("ageTable");
var tableBody = table.getElementsByTagName("tbody")[0];
for (var i = 0; i < PEOPLECOUNT; i++) {
var newRow = document.createElement("tr");
newRow.setAttribute("id", "ageRow" + i.toString(10));
var td1 = document.createElement("td");
var td2 = document.createElement("td");
var td3 = document.createElement("td");
td1.setAttribute("class", "dataCell");
td2.setAttribute("class", "dataCell");
td3.setAttribute("class", "dataCell");
newRow.appendChild(td1);
newRow.appendChild(td2);
newRow.appendChild(td3);
tableBody.appendChild(newRow);
}
}
function generatePeople() {
var firstNames = ["Jack", "Will", "Josh", "Tom", "Sam", "Chloe", "Emily", "Sophie", "Lily", "Olivia"];
var surnames = ["Smith", "Jones", "Brown", "Taylor", "Johnson", "White"];
var locationNames = ["Canyonville", "Hailsmere", "Northpath", "Gracemont", "Gainsburgh", "Heathersmith"];
for (var i = 0; i < PEOPLECOUNT; i++) {
var name = firstNames[randInt(firstNames.length - 1)] + " " + surnames[randInt(surnames.length - 1)];
var location = location[randInt(locationNames.length - 1)];
var age = randInt(100);
var currentPeople = new People(name, location, age);
people.push(currentPeople);
}
loadPeopleIntoTable();
}
function loadPeopleIntoTable() {
for (var i = 0; i < PEOPLECOUNT; i++) {
var people = people[i];
var peopleRow = document.getElementById("ageRow" + i.toString(10));
var cells = peopleRow.getElementsByTagName("td");
for (var j = 0; j < cells.length; j++) {
if (cells[j].hasChildNodes()) {
cells[j].removeChild(cells[j].childNodes[0]);
}
}
cells[0].appendChild(document.createTextNode(people.name));
cells[1].appendChild(document.createTextNode(people.location));
cells[2].appendChild(document.createTextNode(people.age.toString(10)));
}
}
function randInt(maxVal) {
return Math.floor(Math.random() * (maxVal + 1));
}
function commonFirstName() {
var tempArray = [];
var fName;
var array = ['John Smith', 'Jane Smith', 'John Black'];
for (i = 0; i < array.length; i++) {
fName = array[i].split(' ').slice(0, -1).join(' ');
tempArray.push(fName);
}
var mostCommon;
var occurences = 0;
for (j = 0; j < tempArray.length; j++) {
var tempName = tempArray[j];
var tempCount = 0;
for (k = 0; k < tempArray.length; k++) {
if (tempArray[k] == tempName) {
tempCount++;
}
if (tempCount > occurences) {
mostCommon = tempName;
occurences = tempCount;
}
}
}
alert(mostCommon + " : " + occurences);
}
現在、これは関数内にある配列 fullNames で機能しますが、名前、場所、年齢 (最初に示したように) を持つ People オブジェクトで構成される人の配列では機能しません。要素を分割できるように、その配列を通過させる必要があるだけです-_-