Finding the shortest word in an array

I don’t know why its not printing the shortest word for I am sure that I’ve wrote the correct algorithm for this code:

When you split the array up, you do it by spaces. In your original example, you have two spaces which result in one of them being counted as a word and being passed in your array of words.

// The input of:
shortestWord("woke up early  person")

// gets turned into:
[ 'woke', 'up', 'early', '', 'person' ]

That '' is now a string with a length of 0, making it the shortest word and is returned as such

Your second issue is this line of code right here:

    var shortestWord="";

This follows the same logic as the first issue. shortestWord now has a string value with a length of 0. This makes every other word in your words array bigger than it.

I used the console command inside the loop to show this:

console.log(word.length, word, shortestWord, shortestWord.length)

Here is the output:

4 'woke' '' 0
2 'up' '' 0
5 'early' '' 0
6 'person' '' 0

Now try to fix this. Make the initial value of your shortestWord variable as something inside the words array.

How you get " console.log(word.length, word, shortestWord, shortestWord.length)
" to print outside the function since word is not defined? I tried reducing the space for shortest array and increases the space for str.split(" ") and still run into the same issue.

You need to account for any number of consecutive space characters. One way to do that is using a regular expression as the argument for the split method. Another way is to not compare the empty strings. You can add a second condition to your if statement to accomplish this.

You need to assign a value to shortestWord that represents the first real word in the string. You can not assign it an empty string, because no real word in the string would ever be less than an empty string.

I used repl.it because it is easy to debug: https://repl.it/@Michael_Nicol/Debug-Playground

I did it inside the loop:

    for(let word of words){
      console.log(word.length, word, shortestWord, shortestWord.length)
        if(word.length<shortestWord.length){
            shortestWord=word;
        }
    }

word is defined using let which means that it will be defined only within the loop. If you used var, it would be available throughout the entire function.

Could also use a if statement to ignore words elements (element is a item in a array) that have 0 length.

I wrote code that returns the longest word in an array and it works fine:

function longestWord(str){
    let words=str.split(" ");
    let longestWord="";

    for(let word of words){
        if(word.length>longestWord.length){
            longestWord=word;
        }

    }

    return longestWord;
}

console.log(longestWord("woke up early today"));

But when I write the same code for the shortest array , it doesn’t work. I made these most recent

changes and it still doesn’t work.

function shortestWord(str){
    let words=str.split('  ');
    let shortestWord=words[0];
    for(let word of words){
        if(word.length<shortestWord.length){
            word=shortestWord;
        }

        // console.log(word.length, word, shortestWord, shortestWord.length)

    }
 
    return shortestWord;
}


console.log(shortestWord("woke up early today"));

I try assgin words[0] to shortest array variable and still get the same issue

This will only split the string into words that are separated by 2 spaces.

Look at my last reply for how you could approach this without using a regular expression. You are close, but your split is incorrect and your are missing an extra if statement condition.

fixed it. Thanks that did the trick.

Make sure your function can correctly return the shortest word of the following call:

shortestWord("   Yesterday I woke up earlier   than today"); // should return "I"

Notice the spaces at the beginning.

Now, if the string can also have punctuation, you will need a different approach to capture the shortest word.

shortestWord("  Randell typically dines out, though sometimes does dine inside."); // needs to return "out"

The reason it wasn’t an issue with your longest word code is that your initial value was set to a string with a length of 0 which was "".

So, of course, every word you check after that will have a length longer than 0.

Hit the solution button (checkmark in the bottom right-hand corner of a reply that is the most helpful) so people know that your issue has been solved.

That makes sense now looking back at that problem.

1 Like

That is not necessary, but is appreciated.

Hey Following is modified verison of the Program.

function shortestWord(str){
   //Trim whitespaces from rear and front of the string
   console.log("Original String:" + str);
    str = str.trim();

   console.log("Trimmed String:" + str);
    let words=str.split(" ");
//Assign first word to shortest string 
   let shortestWord=words[0];
     
    for(let word of words){
      //Check for Non Empty Words for avoid null string
      if(word.length !== 0){
        if(word.length<shortestWord.length){
            shortestWord=word;
        }
      }
    }
    return shortestWord;
}
console.log(shortestWord("     woke up early today  BITCHES         "));

Let me know if you find any issue with this version