Find the Shortest word in a String
Variant on Longest word
by Jason Land
JavaScript
//find the shortest word in a string
function LongestWord(sen) {
var aSentenceLength = sen.split(/[ ,!?;:.']/);
var vLWArrayPosition = 0;
var vShortestWord = 1000;
for (i = 0; i < aSentenceLength.length; i++) {
var vTestWord = aSentenceLength[i];
if (vTestWord.length < vShortestWord) {
vShortestWord = vTestWord.length;
vLWArrayPosition = i;
}
}
if(vLongestWord == 1){
alert(alert(aSentenceLength[vLWArrayPosition] + " is " + vShortestWord + " character long."));}
else { alert(aSentenceLength[vLWArrayPosition] + " is " + vShortestWord + " characters long.");}
}
LongestWord("The world is not enough!");