に私は正常配列で最長の単語を検索最長ワード機能を実行します配列のSHORTEST単語を取得します。クリエイティブショートワードのJavaScript
var array2 = ["dog", "cat", "horse", "hsalsaaaaa"];
var shortest = 0;
var shortestWord = " ";
for (var i = 0; i < array2.length; i++) {
if (array2[i].length < shortest) { // if an item length is less than 0 then
shortest = array2[i].length; // shortest is equal to the length of the item
shortestWord = array2[i]; // then " " will be the item itself;
}
}
console.log("The shortest word is "+ shortestWord);
console.log("The length of the word is " + shortest);
しかし、このコードは0と空白の単語を送信し続けます。どのようなアイデアをここで微調整するか?
ありがとうございます!
うーん... 0より小さい何ですか? –
一番短い単語を取得するのは大丈夫ですか? –