var people = new Array();
function People (name, location, age){
this.name = name;
this.location = location;
this.age = age;
}
I have two other functions to create and insert people data into a table.
function generatePeople(){}
function loadPeopleIntoTable(){}
My goal is to analyze the list of people and identify the most common first names. This functionality is encapsulated within the commonFirstName() function.
function commonFirstName(){}
The issue I am facing revolves around accessing the 'people' array within the commonFirstName() function. Despite having the code to iterate through the array and find common names, it only seems to work with manually created arrays instead of the 'people' array. Why is that?
The hint suggests that "JavaScript objects can be indexed by strings," but I'm still unclear on its application in this context. Although I've written the code for identifying common first names, I'm unable to utilize the 'people' array within the function.
function commonFirstName(){
alert(people[1]);
//Additional logic for determining common names
}
When executed, the output displays as [object Object]. However, if I use:
function commonFirstName(){
tempArray = ['John Smith', 'Jane Smith', 'John Black'];
//Logic for finding common names here
}
An alert message showing "Common Name: John. Occurs 2 times" is generated.
I attempted passing the 'people' array to the function like so:
function commonFirstName(people){
alert(people[1]);
}
This should ideally return some output related to element 1's full name, location, and age, but alas, there's no response at all. It almost feels as though the array is either empty or does not exist.
The entirety of my code is structured below:
(previous code snippet)At present, while the algorithm successfully identifies common names when using a predefined fullNames array, integrating the 'people' array - which encompasses People objects with name, location, and age properties - remains a challenge. All I require is to pass this array through so I can manipulate the elements accordingly.