*This is the second post in a series of at least 10 code challenges. You can find the others here.*

**Challenge: **Given an array of at least three strings, the function secondGreatest should return the second highest length value. For example, if you were given [“Three “, “Four “, “Fifteen”], the function should return “Five,” which is the length of the second longest item in the array (“Fifteen” would be the longest). Given an array of [“One”, “Two”, “Ten”, “Eleven”], the function should return “Three.” The array will always consist of at least three items.

Here’s my solution collapsed:

<script type="text/javascript"> function secondGreatest(strArr) { // Only proceed for arrays with at least three items if (strArr.length >= 3) { // Initially set secondGreatestValue to 0 var secondGreatestValue = 0; // Loop over words for (i=0; i<strArr.length; i++) { // Assign length variable to current array item length var length = strArr[i].length; // Make sure we don't if statement on first go through if (i!=0) { // If the length of the new string is greater than or equal to the current max if (length > max) { // Set secondGreatestValue equal to previous max secondGreatestValue = max; // Set max equal to length of current string max = length; // If the length of the new string is less than the current max & greater than the secondGreatestValue value } else if (length < max && length > secondGreatestValue) { // Set secondGreatestValue equal to length secondGreatestValue = length; }; // Initially, set the max value to the length of array item at index 0 } else { max = strArr[0].length; }; }; return secondGreatestValue; // If the array has fewer than three items, throw a console error } else{ console.error("There needs to be three items in the array"); }; }; // Log out results of thirdGreatest console.log(secondGreatest(["One", "Two", "Ten", "Four"])); </script>

To start, I setup an if block to test whether the array length was equal to or greater than three. If not, I threw a console error. If it was, I set the secondGreatestValue to zero initially. Then, I looped over the items in the array with a for loop.

In the for loop, I assigned the variable length equal to the length of the current array item. I have another if block that checks to see if this is the first time through the loop (i=0). If so, the value of max is set to the length of array item in position 0. If not, another if block runs.

Initially, this checks if the length of the current item is greater than the current max value. If so, it sets the value of secondGreatestValue to the previous max and resets the max value. If not, the else if block checks to see if the length is less than the max (redundant) and also greater than the current secondGreatestValue. If so, it sets the length as the value for secondGreatestValue.

With this setup, the return value will be 0 if all three items are the same length (since secondGreatestValue is initially set to 0).

**Bonus Challenge:** Instead of returning the second greatest length, return the specific array item with that length. For example, if you were given [“Three “, “Four “, “Fifteen”], the function should return “Three” (not “Five”), which is the array item with the second greatest length. If you were given [“Three “, “Four “, “Fifteen”, “Seven”], it should return “Seven” since both “Three” and “Seven” have a length of five, but “Seven” occurs later on in the array.

In the previous example, we could cheat a bit. We were only concerned with the length returned by the function. So, for example, if we had [“One”, “Two”, “Three”, “Ten”], the secondGreatestValue would be three, and that’s what you would get if you ran the function. However, the function is actually returning the value of the first array item since we never ran a check to see if the length was equal to the current secondGreatestValue. This isn’t a problem since the length of “One” and “Ten” are the same. However, if we want to return the actual item in the array with the second greatest length, we have to do a bit more work.

<script type="text/javascript"> function secondGreatest(strArr) { // Only proceed for arrays with at least three items if (strArr.length >= 3) { // Loop over words for (i=0; i<strArr.length; i++) { // Assign length variable to current array item length var current = strArr[i]; // Establish variable for secondGreatestValue var secondGreatestValue; // Make sure we don't if statement on first go through if (i!=0) { // If the length of the new string is greater than or equal to the current max if (current.length > max.length) { if (max.length > secondGreatestValue.length) { // Set secondGreatestValue equal to previous max array item secondGreatestValue = max; // Set max equal to current array item max = current; } else { max = current; } // If the length of the new string is less than the current max & greater than the secondGreatestValue value } else if (current.length < max.length && current.length > secondGreatestValue.length) { // Set secondGreatestValue equal to current array item secondGreatestValue = current; } else if (current.length == secondGreatestValue.length) { secondGreatestValue = current; }; // Initially, set the max and secondGreatestValue value to the length of array item at index 0 } else { max = current; secondGreatestValue = current; }; }; // If the array has fewer than three items, throw a console error } else{ console.error("There needs to be three items in the array"); }; return secondGreatestValue; }; // Log out results of thirdGreatest console.log(secondGreatest(["One", "Two", "Ten", "Four", "Two"])); </script>

[…] get it working so I moved on to another after an hour of frustration. Reflecting back on the “Second Greatest Length” challenge, I had a little epiphany of how it could […]