skip to Main Content

I have been tasked with this question:

Simple, given a string of words, return the length of the shortest word(s).

String will never be empty and you do not need to account for different data types.

I wrote a code as follows:

function findShort(s){
  //var word = s.split(' ')
  var result = parseInt(word.map(a => a.length))
  return result
  return Math.min(result)
}

the output keeps bring the length of the first word instead of the shortest word.
What might be the problem?

4

Answers


  1. parseInt(word.map(a => a.length)) this will convert the result of map() into an integer, you don’t want that.

    Also, you can’t return twice from the same function call, so the second return will never be reached.

    Math.min() excepts multiple values as arguments, not an array, use the spread syntax (...) to pass the values as arguments instead of an array

    function findShort(s){
      const word = s.split(' ')
      const result = word.map(a => a.length);
      return Math.min(...result);
    }
    
    const res = findShort('hello be world Foo');
    console.log(res);

    That said, you could also use reduce() to compare 2 words, and only ‘keeping’ the length of the shorter one:

    function findShort(s){
        return s.split(' ').reduce((p, c) => p < c.length ? p : c.length, Infinity);
    }
    
    const res = findShort('hello be world Foo');
    console.log(res);
    Login or Signup to reply.
  2. Simply iterate but the thing here I believe is to what thing to initialize. Thinking about a little bit, You can discover You can need to initialize somewhere depending on how you are iterating the list. In my case, I decided to initialize with the first word.

    function searchWord(strings){
       let words = strings.split(' ');
       let min = words[0].length;
       let word=words[0];
       for (w of words){
         if ( w.length < min) {
             word = w;
         }
       }
       return word;
    }
    
    searchWord("aw ab abc d");
    
    Login or Signup to reply.
  3. Since apostrophes do not typically count toward the length of a word in most contexts, you should probably add an option to handle them.

    I added a prune flag to the function to remove non-word characters when determining the length of a word.

    const findShortestWordLength = (phrase, prune = false) =>
      phrase.match(/bw+('w+)?b/g)            // Match all words (and contractions)
        .reduce((minLength, word) =>             // Reduce each word to a min word length
          Math.min(minLength, prune              // Store the min length
              ? word.replace(/[W]/g, '').length // Remove apostrophes
              : word.length)                     // Keep the original in-tact
            , Number.MAX_VALUE);                 // Initialize with max; we want a min
    
    // No pruning
    console.log(findShortestWordLength("It's party time!"));       // 4
    console.log(findShortestWordLength("It's a Wonderful Life!")); // 1
    
    // With pruning
    console.log(findShortestWordLength("It's party time!", true)); // 3
    Login or Signup to reply.
  4. Split and reduce!

    function findShortestWordLength(input) {
     const words = input.split(' ');
     const minLength = words.reduce((min,word)=>Math.min(min,word.length),Infinity);              
     return minLength;
    }
    
    console.log("The shortest word length is:",findShortestWordLength("One Two Three Four"));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search