简体   繁体   中英

find the shortest word in an array

id like to get the shortest word in the array. this returns the first word which is "bil" even though "ei" is the shortest. how can i define a word is shorter than the others?

const words= ['bil', 'plante', 'kaffe', 'bog', 'ei', 'planetarium'];

function getShortestWord() {
  for(let i = 0; i < words.length; i++) {
    const strSplit = words[i].split('');
    let lengthOfWord = strSplit.length;

    if(strSplit[i].length < lengthOfWord) {
      lengthOfWord = strSplit[i].length
    }
    return lengthOfWord
   }
}
getShortestWord()

Sort array by their length.

 const words = ["bil", "plante", "kaffe", "bog", "ei", "planetarium"]; let ret = words.sort((x, y) => x.length - y.length); console.log(ret[0]);

 const wordsArray = [ 'bil', 'plante', 'kaffe', 'bog', 'ei', 'planetarium' ]; function shortestWordInArray(words){ let result = ""; words.forEach( word => { word.length < result.length ? result = word : null } ) console.log(result) return result; } shortestWordInArray(wordsArray)

This would be my approach, iterate the items and store the lengthier.

You need to fix the return and split statement. You are using array, so don't need split statement.

function getShortestWord() {
  if(words == null || words.length < 1) { 
    return words; // handling empty array
  }

  let minLengthWord = words[0];
  for(let i = 1; i < words.length; i++) {
    if(words[i].length < minLengthWord.length) {
       minLengthWord = words[i];
    }
   }
   return lengthOfWord
   
}

Note: the return statement is outside the for loop. As we need to find shortest word, which we will know only when we traverse whole array.

You need to actually track and then return the word that you determine to be shortest. Also, string.length will directly give you the length. Note that I'm trying to stick as close to your concept as possible, for your learning purposes. :)

const words= ['bil', 'plante', 'kaffe', 'bog', 'ei', 'planetarium'];

function getShortestWord() {
    let shortest = 0;
    for(let i = 0; i < words.length; i++) {

        if(words[i].length < words[shortest].length) {
            shortest = i
        }
    }
    return words[shortest]
}

let shortest = getShortestWord()
console.log("Shortest = " + shortest)

Just loop over the array storing the first element as the current shortest word to compare with, if another string has a shorter length then make it the current shortest word else just continue looping and check, finally return that word as it is the shortest word in that array

 const words = ['bil', 'plante', 'kaffe', 'bog', 'ei', 'planetarium']; function getShortestWord(w) { let shortestWord = w[0]; for(let i = 1;i < w.length; i++) { // it's just short circuit condition shortestWord.length > w[i].length && (shortestWord = w[i]); } return shortestWord; } console.log(getShortestWord(words));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM