-2

I Have an array name friends. I want to print all the (not single) name which are tiny from this array. How i can solve it??:

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

    }
    console.log("The Smallest Name is",tinyFriend)
}

let friends = ["kamal","shak","shak","shakib", "brac"]
tinyFriend(friends)

my expected result is shak,shak,brac

6
  • What is your expected output? Commented Aug 8, 2022 at 14:06
  • 1
    Your code already returns the shortest name (in a way ...), so what's your desired output? Commented Aug 8, 2022 at 14:07
  • my expected result is shak,shak,brac . i want to print all the name which length are smallest Commented Aug 8, 2022 at 14:10
  • How to sort an array based on the length of each element?, only in opposite order - and then you loop over the result and output the elements, as long as the length does not change from one to the next (at that point you got to stop.) Commented Aug 8, 2022 at 14:12
  • Or you simply determine the minimum length first, and then you loop over your (original) array again, and output only the elements that have their own length matching that minimum. Commented Aug 8, 2022 at 14:12

2 Answers 2

0

you basically need to make tinyFriend an array, and add some logic like:

  • is the current name the same length as the one being checked? if so add it to the array
  • is the checked name smaller than the current name(s)? in that case overwrite the array

function tinyFriend(friends) {
    // put the first name in an array to begin..
    let tinyFriends = [friends[0]];
    
    // loop the names in the array
    for (let i = 0; i < friends.length; i++) {
        let element = friends[i]
        
        // check if same length and add to array if so
        // notice we now check against the first item in the tinyFriends array
        if (tinyFriends[0].length === element.length) {
          tinyFriends.push(element);
        
        // else if the checked name is shorter, replace the array 
        } else if (element.length < tinyFriends[0].length) {
          tinyFriends  = [element];
        }
    }
    
    // remember, since we now output an array, we might need to handle it differently
    console.log("The Smallest Name is", tinyFriends)
}

let friends = ["kamal","shak","shak","shakib", "brac"]
tinyFriend(friends)

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

1 Comment

Thank You mate. I dont know how to use stackoverflow properly. This is my first day and thanks god that i get the result i have wanted.
0

You need to be able to store the names somewhere that match the critera - ie in an array.

But it might be easier to find the minimum name length first and then filter out the names whose length match that value.

function tinyFriend(friends) {

  // `map` over the array and return an array of name lengths
  // then use `Math.min` to determine the smallest value
  // in that array. `Math.min` expects a list of arguments
  // so we use the spread syntax on the array
  const min = Math.min(...friends.map(name => name.length));

  // Now `filter` out the names whose length matches that value
  return friends.filter(name => name.length === min);

}

let friends = ['kamal', 'shak', 'shak', 'shakib', 'brac'];
console.log(tinyFriend(friends));

Additional documentation

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.