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
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