0
  1. Find the person who has many skills in the users object.

  2. Count logged in users, count users having greater than equal to 50 points from the following object

const users = {
  Alex: {
    email: 'alex@alex.com',
    skills: ['HTML', 'CSS', 'JavaScript'],
    age: 20,
    isLoggedIn: false,
    points: 30
  },
  Asab: {
    email: 'asab@asab.com',
    skills: ['HTML', 'CSS', 'JavaScript', 'Redux', 'MongoDB', 'Express', 'React', 'Node'],
    age: 25,
    isLoggedIn: false,
    points: 50
  },
  Brook: {
    email: 'daniel@daniel.com',
    skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux'],
    age: 30,
    isLoggedIn: true,
    points: 50
  },
  Daniel: {
    email: 'daniel@alex.com',
    skills: ['HTML', 'CSS', 'JavaScript', 'Python'],
    age: 20,
    isLoggedIn: false,
    points: 40
  },
  John: {
    email: 'john@john.com',
    skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux', 'Node.js'],
    age: 20,
    isLoggedIn: true,
    points: 50
  },
  Thomas: {
    email: 'thomas@thomas.com',
    skills: ['HTML', 'CSS', 'JavaScript', 'React'],
    age: 20,
    isLoggedIn: false,
    points: 40
  },
  Paul: {
    email: 'paul@paul.com',
    skills: ['HTML', 'CSS', 'JavaScript', 'MongoDB', 'Express', 'React', 'Node'],
    age: 20,
    isLoggedIn: false,
    points: 40
  }

Why do I get stuck when solving this kind of problem with arrays and objects? I have done so many projects with the front end(Angular), but can't solve this, I know the logic but am not able to put it in code. please help, need advice on how to get better, do I need to learn Algorithms to get better at this kind of problems?

  • 1
    first convert the obj to an array using Object.entries(). and then you can sort the array based on the length of the skills field to get max. and you can use filter to get the results based on logged in points ==50 and get the length of the filtered array. https://stackoverflow.com/questions/11206393/sort-arrays-by-their-length – cmgchess Nov 05 '22 at 11:16
  • 1/2 ... First one has to convert the `users` object into an array of user items where one would also save each user's name/key ... `Object.entries(users).map(([name, data]) => ({ name, ...data}))`. Then there are several approaches. The most easiest in terms of writing code was to `reduce` the items array by comparing the `skills` array `length` values of both passed items and return the one with the higher `length` value. Another but brutal approach was to `sort` the array items by each item's `skills` array `length` value in descending order and then pick the first item of the sorted array. – Peter Seliger Nov 05 '22 at 11:36
  • 2/2 ... `Object.entries(users).map(([name, data]) => ({ name, ...data})).reduce((result, item) => (result.length > item.length && result || item))/*.name*/;` – Peter Seliger Nov 05 '22 at 11:41
  • How do you sort the array based on the skill's length? by default sorting in js is done on strings. sorting based on the length I need to provide a compare function if I'm not wrong. @cmgchess – Hemanth Girimath Nov 05 '22 at 14:22

1 Answers1

-1

This is how you can get the person who has many skills in the users object

    const compareFn = (a, b) => {

      if(a[1]?.skills?.length < b[1]?.skills?.length) return 1
      return -1
    }

    const array = Object.entries(users);
    const sortedArray = array.sort(compareFn);

    const person = sortedArray[0][0]; //the name of person person who has many skills
    const personValues = sortedArray[0][1]; //the values of person person who has many skills


    const [personName, personValues] = sortedArray[0]; //get the name and values of person

    const sortedObject = Object.fromEntries(sortedArray) //Returns a sorted object

The techniques used in this solution

function compareFn(a, b) {
  if (a is less than b by some ordering criterion) {
    return -1;
  }
  if (a is greater than b by the ordering criterion) {
    return 1;
  }
  // a must be equal to b
  return 0;
}
Layeb Mazen
  • 142
  • 5
  • hey, it works thanks for the solution. Can you please direct me on how I can get better at this kind of problem-solving? I'm really struggling – Hemanth Girimath Nov 05 '22 at 14:30
  • and can you please explain the comparefn . I don't understand, you are comparing only the first 2 values . But how does it work, you didn't even increment – Hemanth Girimath Nov 05 '22 at 14:32
  • @HemanthGirimath … Why does the OP at all accept an answer where the OP does not seem to fully understand the approach and its implementation. The `sort` approach btw is not very effective for retrieving the name of the most skillful person. And why did the OP not try to implement or comprehend some of the suggestions/solutions from the above comments? – Peter Seliger Nov 05 '22 at 14:47
  • I'm sorry. But the solution worked for me. I even tried altering the values and cross check if it works or not. I even gave a reply to your answer. and @PeterSeliger – Hemanth Girimath Nov 05 '22 at 14:56
  • @Layeb Mazen gave me the code and its explanation making it very easy to understand. that's the reason I accepted his answer – Hemanth Girimath Nov 05 '22 at 14:58
  • Hey @HemanthGirimath, i update my post you can check the links to get more information – Layeb Mazen Nov 05 '22 at 15:59
  • @HemanthGirimath … _“the solution worked for me“_ is not a valid criteria in case one still struggles with the basics. … _“I even gave a reply to your answer“_ … apparently you didn‘t. And the 2nd counting problem of the OP‘s question still has no approach nor answer. – Peter Seliger Nov 05 '22 at 17:57
  • @HemanthGirimath ... The most straightforward and also more accurate (in terms of return values including `0`) implementation of the above answer's `compareFn` was more like ... `const compareFn = (a, b) => b[1].skills.length - a[1].skills.length;` – Peter Seliger Nov 07 '22 at 14:44