How to find length of shortest word in an array?

How to find length of shortest word in an array?

I’m trying to find the shortest word in an array. I am using this code

function findShortestWord(str) {
  var strSplit = str.split(' ');
  var shortestWord = 0;
  for(var i = 0; i < strSplit.length; i++){
    if(strSplit[i].length > shortestWord){
	shortestWord = strSplit[i].length;
  return shortestWord;

which actually finds the longest word. I just need to change it up to find the smallest word. I’m having trouble figuring out what to set the shortestWord variable to and how i should implement that on the if statement

You should initiate your longestWord variable to infinity to have the maximum number.

Your Conditional Statement should be if array-element-length (strSplit[i].length) is less than infinity (longestWord)
strSplit[i].length < longestWord

if true then:
change longestWord value to the length of the current array-element.
longestWord = strSplit[i].length

And then change shortestWord value to the current array-element
longestWord = strSplit[i]

and then last return the shortestWord

right now your code doesn’t work because you have longestWord variable not defined. i suggest you first change the variable to shortestWord everywhere so that at least it executes, and then think on the logic

I’m sorry I forgot to change the return statement to return shortestWord; . Don’t know if that changes anything

if that’s the only change you make, then you are not changing shortestWord at all