Created
August 27, 2024 01:41
-
-
Save Vampeyer/b951002d732eb30f1216f794cee04c92 to your computer and use it in GitHub Desktop.
JavaScript Mastery - Kata 4 , longest name in nested obj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Kata 4 - Instructor's Names | |
// Assignment | |
// 1h | |
// Status | |
// Incomplete | |
// In this exercise, you will be given a list of instructors and you will 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"} | |
let instructorNames = [ | |
{ name: "Samuel", course: "iOS" }, | |
{ name: "Jeremiah", course: "Web" }, | |
{ name: "Ophilia", course: "Web" }, | |
{ name: "Donald", course: "Web" }, | |
]; | |
function instructorWithLongestName(instructorNames){ | |
let returnArray = [] | |
let longest = 0 | |
for( let i in instructorNames){ | |
let theLengths = instructorNames[i]["name"].length | |
if( theLengths > longest){ | |
longest = theLengths | |
} | |
if(longest === theLengths){ | |
// console.log(instructorNames[i]["name"]) | |
returnArray.push(instructorNames[i]["name"]) | |
} | |
} | |
return returnArray.pop() | |
} | |
console.log(instructorWithLongestName(instructorNames)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment