-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsite01.js
More file actions
47 lines (41 loc) · 1.22 KB
/
Copy pathsite01.js
File metadata and controls
47 lines (41 loc) · 1.22 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
const marvelHeroes = [
"Ant-Man",
"Black Panther",
"Black Widow",
"Captain America",
"Doctor Strange",
"Hawkeye",
"Hulk",
"Iron Man",
"Luke Cage",
"Moon Knight",
"Ms. Marvel",
"Scarlet Witch",
"Spider-Man",
"Thor",
"Wasp"
]
// 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.
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.
function findLongestString(namesArry) {
// Declare a variable to store the longest string
let lstring = "";
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;
}
// Call the driver function to find and display the longest hero
findHero();