0

I am creating a program function in JS that returns the smallest string in array. However I always get an error return.

Here's my code:

function findShortestWordAmongMixedElements(arr) {

  let shortest = '';

  if (arr.length > 0) {
    for (let i = 0; i < arr.length; i++) {
      if (typeof arr[i] === 'string' && arr[i].length < shortest.length) {
        shortest = arr[i];
      }
    }
  }
}
return shortest;
}

var output = findShortestWordAmongMixedElements([4, 'two', 2, 'three']);
console.log(output); // --> 'two'

Any idea what am I doing wrong why my code won't work?

PS. If the given array contains no strings, it should return an empty string.

5
  • Cannot think of a string smaller in length than an empty string Commented Aug 5, 2017 at 14:23
  • Can you let me know where did I go wrong? anyone can fixed my code so I can check it out. Commented Aug 5, 2017 at 14:24
  • let shortest = ''; is empty all the time therefore it is the smallest. This variable seems is not correctly initialize Commented Aug 5, 2017 at 14:24
  • you are initializing shortest with actually shortest length (0) Commented Aug 5, 2017 at 14:25
  • maybe start by initializing it to undefined. Commented Aug 5, 2017 at 14:26

8 Answers 8

1

You have several errors. You have written your return in wrong place. And your finding short string logic is wrong. Take infinity as the shortest and then check against smaller length string.

function findShortestWordAmongMixedElements(arr) {
    let shortLength = Infinity;
    let shortest = "";

    if (arr.length > 0) {
        for (let i = 0; i < arr.length; i++) {
            if (typeof arr[i] === 'string' && arr[i].length < shortLength) {
                shortest = arr[i];
                shortLength = arr[i].length;
            }
        }
    }

    return shortest;
}

var output = findShortestWordAmongMixedElements([4, 'two', 2, 'three']);
console.log(output); // --> 'two'

Sign up to request clarification or add additional context in comments.

3 Comments

What does infinity stands for? what does it do?
@JamesHedegon First assuming infinity is the greatest possible short and then comparing against others.
1

Here's a better implementation of your logic. We can just filter out string arrays and sort it out according to string length and return first element.

function findShortestWordAmongMixedElements(arr) {
    let strings = arr.filter( x => typeof x === "string" )
    .sort((a, b) => a.length - b.length);
    
    // arr.filter gives you elements which are only strings
    // arr.sort returns you elements according to their string lengths. Then you'll just have to return the first element (because it is already smallest)
    
    return strings[0];
}

var output = findShortestWordAmongMixedElements([4, 'two', 2, 'three']);
console.log(output);

2 Comments

Looks like OP is beginner level and consider correcting his code instead dumping working codes. It's worth mentioning that OP should know ES6 to understand this.
@ꜱᴜʀᴇꜱʜᴀᴛᴛᴀ your answer already covered that by the time I answered. I thought to provide more value with a different approach :)
0

Essentially there are two bugs:

  1. The return statement is outside the function definition.

  2. As others have mentioned in the comments, you are initializing the variable shortest to '' which prevents it from taking new value.

function findShortestWordAmongMixedElements(arr) {

  let shortest = undefined;

  if (arr.length > 0) {
    for (let i = 0; i < arr.length; i++) {
      if (typeof arr[i] === 'string' && (shortest == undefined ||arr[i].length < shortest.length )) {
        shortest = arr[i];
      }
    }
  }

  return shortest;
}


var output = findShortestWordAmongMixedElements([4, 'two', 2, 'three']);
console.log(output); // --> 'two'

Comments

0

I believe this should work

function findShortestWordAmongMixedElements(arr) {

    let shortest = null;

    if(arr.length > 0){
        for(let i = 0; i < arr.length; i++){
           if(typeof arr[i] === 'string'){
                if(shortest == null)
                    shortest = arr[i];
                else if(arr[i].length < shortest.length){
                    shortest = arr[i];
                }
            }
        }
    }
    return shortest;
}
var output = findShortestWordAmongMixedElements([4, 'two', 2, 'three']);
console.log(output); // --> 'two'

Comments

0

Here is your function which will find the smallest string:

function findShortestWordAmongMixedElements(arr) {

    let shortest = '';

       if(arr.length > 0){
         for(let i = 0; i < arr.length; i++){
           if(typeof arr[i] === 'string')
             {
               if(shortest.length == 0) {
                   shortest = arr[i]; continue;
               } 
               
               if(arr[i].length < shortest.length){
                   shortest = arr[i]; 
               }
              
            }
          }
         }
          return shortest; 

       }
       
       var output = findShortestWordAmongMixedElements([4, 'two', 2, 'three']);
console.log(output);

Comments

0

It might just be easier if you filter out the array and then reduce it:

function findShortestWordAmongMixedElements(arr) {
  return arr.filter(el => typeof el === 'string')
    .reduce((sht, str) => str.length < sht.length ? str : sht);
}

var output = findShortestWordAmongMixedElements([4, 'two', 2, 'three']);
console.log(output); // --> 'two'

Comments

0

function tinyFriend(arr) {
    var tiny = arr[0];
    for (let i = 0; i < arr.length; i++) {
        var element = arr[i];
        if (tiny.length > element.length) {
            tiny = element;
        }
    }
    return tiny;
}

var friend = ["Rohim", "Korim", "Bokkar", "Jobbar", "Noor"]
var smallFrind = tinyFriend(friend)

console.log("No.4: Your tiny friend name is", smallFrind)

for (let i = 0; i < arr.length; i++) {
    var element = arr[i];
    if (tiny.length > element.length) {
        tiny = element;
    }
}
return tiny;

}

Comments

-1
 function tinyFriend(arr){
            var tiny = arr[0];
             for (let i = 0; i < arr.length; i++) {
                const element = arr[i];
                    if( tiny.length > element.length){
                        tiny = element
                        }
                             }
                            return tiny
                                }

                var  friend = ["abir","abdullah","robin","abdurrohim","ali"]
                var smallFrind = tinyFriend(friend)
                console.log(smallFrind)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.