From bdb5202b7a5da6858bfe3d6c68480b3debe17891 Mon Sep 17 00:00:00 2001 From: jareleze <112780966+JarelEze1@users.noreply.github.com> Date: Thu, 26 Oct 2023 14:25:33 +0100 Subject: [PATCH 1/2] completed the first challenge --- Challenge01/index.html | 7 ++----- Challenge01/site01.js | 30 ++++++++++++++++++++---------- 2 files changed, 22 insertions(+), 15 deletions(-) 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..092d06a 100644 --- a/Challenge01/site01.js +++ b/Challenge01/site01.js @@ -15,22 +15,32 @@ 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 } -//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 From 4d00a20ce202ebd16857a861c20adb323cdb66ce Mon Sep 17 00:00:00 2001 From: jareleze <112780966+JarelEze1@users.noreply.github.com> Date: Thu, 26 Oct 2023 14:35:12 +0100 Subject: [PATCH 2/2] added extra credit for other heroes --- Challenge01/site01.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Challenge01/site01.js b/Challenge01/site01.js index 092d06a..e9e6c32 100644 --- a/Challenge01/site01.js +++ b/Challenge01/site01.js @@ -24,6 +24,7 @@ const marvelHeroes = [ document.getElementById("results").innerHTML = lword; // 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.