-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestNameMethod.js
More file actions
39 lines (34 loc) · 1.05 KB
/
LongestNameMethod.js
File metadata and controls
39 lines (34 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const instructorWithLongestName = function(instructors) {
return instructors.sort(function(a, b) {return b.name.length - a.name.length})[0];
};
const forEachIterator = function(instructors) {
max_lenght = 0;
instructor_index = null;
instructors.forEach(function(instructor, index) {
if (instructor.name.length > max_lenght) {
max_lenght = instructor.name.length;
instructor_index = index;
}
});
return instructors[instructor_index];
};
const traditionalLoops = function(instructors) {
var longest = {name: "", course: ""};
for (var i = 0; i < instructors.length; i++) {
if (instructors[i].name.length > longest.name.length) {
longest = instructors[i];
}
}
return longest;
};
console.log(instructorWithLongestName([
{name: "Samuel", course: "iOS"},
{name: "Jeremiah", course: "Web"},
{name: "Ophilia", course: "Web"},
{name: "Donald", course: "Web"}
]));
console.log(instructorWithLongestName([
{name: "Matthew", course: "Web"},
{name: "David", course: "iOS"},
{name: "Domascus", course: "Web"}
]));