mercredi 9 septembre 2015

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

Aucun commentaire:

Enregistrer un commentaire