Reputation: 21
I am just starting on JavaScript and getting lost on how to deal with this issue.
Here's the problem:
Five people, John, Ed, Sam, Alex and Mark are 16, 25, 18, 30 and 27 years old respectively.
I want to use arrays and functions to output the youngest and oldest of the five.
I tried to form two arrays i.e.
var Name = ['John', 'Ed', 'Sam', 'Alex', 'Mark'];
var Age = [16, 25, 18, 30, 27];
I am finding it difficult to manipulate these arrays to get the required results.
What do I do to these arrays so that it can print for instance, that the youngest person is John and the oldest is Alex?
Upvotes: 2
Views: 1018
Reputation: 140220
You wouldn't normally have data structured that way as you can see it's excessively complicated to do anything with it.
var names = ['John', 'Ed', 'Sam', 'Alex', 'Mark'];
var ages = [16, 25, 18, 30, 27];
If in some hypothetical situation you actually had to work with that, you could normalize it this way:
var people = names.map(function (name, i) {
return {
name: name,
age: this[i]
};
}, ages);
The result of the mapping is same as writing:
var people = [
{"name":"John","age":16},
{"name":"Ed","age":25},
{"name":"Sam","age":18},
{"name":"Alex","age":30},
{"name":"Mark","age":27}
];
(Yes, the property names are quoted because I cheesed it with JSON.stringify
cos I'm lazy )
And sorting becomes really trivial:
people.sort( function( a, b ) { return a.age - b.age; });
var youngestName = people[0].name,
oldestName = people[people.length-1].name;
So if you have control, use proper structure in the first place.
Upvotes: 3
Reputation: 169383
var names = ['John', 'Ed', 'Sam', 'Alex', 'Mark'],
ages = [16, 25, 18, 30, 27];
// reduce the ages array to an object containing youngest and oldest
var data = ages.reduce(function (memo, age, index) {
// for every member in the age array
// if the age is less then the youngest
if (age < memo.youngest.age) {
// then set that to be the youngest age
memo.youngest.age = age;
// and store the name by grabbing it from the names array by index
memo.youngest.name = names[index];
}
// do the same for oldest
if (age > memo.oldest.age) {
memo.oldest.age = age;
memo.oldest.name = names[index];
}
return memo;
// create a default seed for this object,
// the default youngest age is infinite, the default oldest age is minus infinite
}, {
youngest: {
age: Infinity
},
oldest: {
age: -Infinity
}
});
// data object returned has the same format as the initial seed object
console.log(data.youngest.name);
console.log(data.oldest.name);
Upvotes: 1
Reputation: 150020
You can create an array of objects, where each object has a name
and age
property, which would allow you to sort the array by age and thus easily get the youngest or oldest. This is covered by at least one other answer, so I won't repeat it here.
Since you mention you are just starting in JavaScript you might like some general practice looping through arrays and so forth, so for the two arrays you defined in your question if you want the youngest person then loop through the age array to find the location of the lowest age and then output the name from the corresponding location in the name array:
function getYoungest(names, ages) {
if (names.length != ages.length || names.length === 0)
return ""; // stop if arrays were a different length or empty
var i,
y,
a = Number.MAX_VALUE;
for (i=0; i < ages.length; i++) {
if (ages[i] < a) {
a = ages[i];
y = names[i];
}
}
return y;
}
var Name = ['John', 'Ed', 'Sam', 'Alex', 'Mark'],
Age = [16, 25, 18, 30, 27];
console.log(getYoungest(Name, Age)); // 'John'
The above uses a
to keep track of the lowest age so far, and y
to keep the name that goes with that age, but of course you could add additional variables to simultaneously keep track of the highest age and corresponding name so far so that you could output the youngest and oldest with only one pass through the array.
(Of course something similar to the above would work on an array of objects too, if you didn't want to sort the array, or there was some other criteria you were looking for where sorting wouldn't help.)
Upvotes: 0
Reputation: 11779
You need to use array of objects - eg.
var People = [{ name : 'John', age : 16 }, .... ]
So you can keep age and name together. Then use sort function with custom sorting function
var PeopleSorted = People.sort( function( a, b ) { return a.age - b.age; });
PS. More on Array.sort here - https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort
Upvotes: 4
Reputation: 9202
var Name = ['John', 'Ed', 'Sam', 'Alex', 'Mark'];
var Age = [16, 25, 18, 30, 27];
var AgeFromName = {};
for (var i = 0; i < Age.length; i++) {
AgeFromName[Age[i]] = Name[i];
}
Age.sort();
alert(AgeFromName[Age[0]]);
Upvotes: 0