-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstructorsNames.js
35 lines (26 loc) · 941 Bytes
/
instructorsNames.js
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
const instructorWithLongestName = function(instructors) {
let nameLength = [];
let lengthCount = 0;
let longIndex = 0;
let longName;
for (obj of instructors) {
nameLength.push(obj.name.length);
}
// for loop to find largest nameLength value - store to lengthCount
for (let i = 0; i < nameLength.length; i++) {
if (nameLength[i] > lengthCount) {
lengthCount = nameLength[i];
}
}
// Use .indexOf to find index of largest lengthCount value - store to longIndex
longIndex = nameLength.indexOf(lengthCount);
// Use longIndex (which should match index of obj in array with largest name length value) to store object to longName variable
longName = instructors[longIndex];
// Test nameLength, lengthCount, longIndex, longName
console.log(nameLength);
console.log(lengthCount);
console.log(longIndex);
console.log(longName);
// Return obj with longest name
return longName;
};