Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreiskandar/19bf4a9b00c247c29f63d73e8f81f7ff to your computer and use it in GitHub Desktop.
Save andreiskandar/19bf4a9b00c247c29f63d73e8f81f7ff to your computer and use it in GitHub Desktop.
Determine instructors who have longest name in the object
/*
In this exercise, we will be given a list of instructors and have to determine which instructor has the longest name.
Input
const instructorWithLongestName = function(instructors) {
// Put your solution here
};
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"}
]));
Expected Output
{name: "Jeremiah", course: "Web"}
{name: "Domascus", course: "Web"}
*/
const instructorWithLongestName = function(instructors) {
let longestName = instructors[0].name.length;
let name = '';
for(let i = 1; i < instructors.length; i++) {
if(instructors[i].name.length > longestName) {
longestName = instructors[i].name.length; //?
name = instructors[i].name; //?
course = instructors[i].course;
}
}
return { 'name': name, 'course': course};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment