diff --git a/Challenge01/index.html b/Challenge01/index.html index a7dc34c..d664b81 100644 --- a/Challenge01/index.html +++ b/Challenge01/index.html @@ -1,12 +1,9 @@ - - JS Challenge 01 - - + JS Challenge 01 @@ -54,7 +51,7 @@

Longest String in an Array

- + js Challengeimage
diff --git a/Challenge01/site01.js b/Challenge01/site01.js index 7456796..e9e6c32 100644 --- a/Challenge01/site01.js +++ b/Challenge01/site01.js @@ -15,22 +15,33 @@ const marvelHeroes = [ "Thor", "Wasp" ] -//driver function used for display and passing values. -function findHero() { - - //implement the function findLongestString that returns the longest word. + // Driver function used for display and passing values. + function findHero() { + // Implement the function findLongestString that returns the longest word. let lword = findLongestString(marvelHeroes); - //used for display. no need to change + // Used for display. document.getElementById("results").innerHTML = lword; - //extra credit display all of the heroes to the page - + // Extra credit: display all of the heroes to the page + document.getElementById("namelist").innerHTML = marvelHeroes.join(" | "); } -//takes an array of strings and returns the longest one. +// Takes an array of strings and returns the longest one. function findLongestString(namesArry) { + // Declare a variable to store the longest string + let lstring = ""; - return ""; + for (let index = 0; index < namesArry.length; index++) { + if (namesArry[index].length > lstring.length) { + // Update lstring with the longer string + lstring = namesArry[index]; + } + } + + // Return the longest string + return lstring; +} -} \ No newline at end of file +// Call the driver function to find and display the longest hero +findHero(); \ No newline at end of file