Javascript: Finding the longest name from an array
I am trying to learn javascript and am just beginning.
So I have created my own project. I have an array of Rivers and want to compare the lengths of their names.
I know it IS NOT written in code form - since I am beginning - I like to write in english what I have to do.
I'm not sure how to set up the if/else statements with this large a comparison.
Can anyone help with Step 3 by pointing me in the right direction without spoonfeeding me an answer?
Thanks!
STEP 1 - List River names
STEP 2 - List River names lengths
STEP 3 - List the longest River and list the # of letters in the name
STEP 4 - List the Shortest River and list # of letters
STEP 5 - List the number of letters difference between the longest and the shortest ~ Modulo
/STEP 1/ - Done
var rivers = ["Mississippi","Delaware","Ohio","Sangamon","Black","Current","Chattahochee"];
for (var i = 0; i < rivers.length; i++) {
console.log("An interesting "+rivers[i]+" River fact:")
;
/STEP 2/ - Done
console.log("The "+rivers[i]+" River's name contains "+ (rivers[i].length)+" letters in it.")
;
}
/STEP 3/ HERE I want to compare the letter strings to find the longest name in the array. I then want the output to say “The river with the longest name is the X River, and it has Y letters." But I do not understand how to set up the if-else statement.
for (var i = 0; i < rivers.length; i++) {
If A.length > B.length Keep A else keep B
If A.length > C.length Keep A else keep C
If B.length > C.length Keep B else keep C
If A.length > D.length Keep A else keep D
If B.length > D.length Keep B else keep D
If C.length > D.length Keep C else keep D
If A.length > E.length Keep A else keep E
If B.length > E.length Keep B else keep E
If C.length > E.length Keep C else keep E
If D.length > E.length Keep D else keep E
If A.length > F.length Keep A else keep F
If B.length > F.length Keep B else keep F
If C.length > F.length Keep C else keep F
If D.length > F.length Keep D else keep F
If E.length > F.length Keep E else keep F
If A.length > G.length Keep A else keep G
If B.length > G.length Keep B else keep G
If C.length > G.length Keep C else keep G
If D.length > G.length Keep D else keep G
If E.length > G.length Keep E else keep G
If F.length > G.length Keep F else keep G
You're on the right track, but you're overthinking this; just keep track of the longest one when you're looping. If you find one that's longer, update it. I'll even psuedocode this for you so you can see:
myfunc() {
var longest = "";
for <all your things> {
if <thing>.length > longest {
longest = thing
}
}
//longest contains our longest thing!
//you can get the length by doing longest.length
}
Sort the list of rivers from the shortest to the longest:
rivers.sort(function(a, b) {
return a.length - b.length;
});
Then the shortest is rivers[0]
and the longest is rivers[length - 1]
.
This is not the ideal solution, but it's good enough for learning.
You could use reduce
. It allows you to iterate through an array and carry a value from the previous iteration to the next. You can use that ability to store the longest name, and replacing it when you come across an even longer name.
var longestName = rivers.reduce(function(longName, maybeLongerName){
// What you return here, becomes "longName" in the next iteration
// If it's the last item, what you return is the value returned by reduce
return maybeLongerName.length > longName.length ? maybeLongerName : longName;
}, '');