I couldn’t find answer for this anywhere this is my last hope .
(stuck in learning!!!)
Find user with most skills .
I have no clue how I can use for loop here !!
I tried —–
x=[]
users.getfuction = function(){
x.push(users.Alex.skills.length)
x.push(users.Asab.skills.length)
x.push(users.Brook.skills.length)
x.push(users.Daniel.skills.length)
x.push(users.John.skills.length)
x.push(users.Paul.skills.length)
x.push(users.Thomas.skills.length)
max=x[0]
for(i=1;i<x.length;i++){
if(x[i]>max){
max=x[i]
}
}
return z
}
console.log(users.getfuction())
//I dont want to keep pushing the name one by one!!
const users = {
Alex: {
skills: ['HTML', 'CSS', 'JavaScript']
},
Asab: {
skills: ['HTML', 'CSS', 'JavaScript', 'Redux', 'MongoDB', 'Express', 'React', 'Node']
},
Brook: {
skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux']
},
Daniel: {
skills: ['HTML', 'CSS', 'JavaScript', 'Python']
},
John: {
skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux', 'Node.js']
},
Thomas: {
skills: ['HTML', 'CSS', 'JavaScript', 'React']
},
Paul: {
skills: ['HTML', 'CSS', 'JavaScript', 'MongoDB', 'Express', 'React', 'Node']
}
}
>Solution :
returns an array of a given object’s own enumerable string-keyed property [key, value] pairs.
The pseudo-code of this is
loop through each user:
if current_user's skill count is greater than the record:
set current_user as the best user
save current_user's skill count for comparison
print out best user
[name, {skills}] is a thing called destructuring. This is the same as saying:
for (const entry of Object.entries(users)) {
const name = entry[0];
const skills = entry[1].skills;
if (skills.length > bestUserSkills) {
bestUserName = name;
bestUserSkills = skills.length;
}
}
const users = {Alex:{skills:['HTML','CSS','JavaScript']},Asab:{skills:['HTML','CSS','JavaScript','Redux','MongoDB','Express','React','Node']},Brook:{skills:['HTML','CSS','JavaScript','React','Redux']},Daniel:{skills:['HTML','CSS','JavaScript','Python']},John:{skills:['HTML','CSS','JavaScript','React','Redux','Node.js']},Thomas:{skills:['HTML','CSS','JavaScript','React']},Paul:{skills:['HTML','CSS','JavaScript','MongoDB','Express','React','Node']}};
let bestUserName = "";
let bestUserSkills = -1;
for (const [name, {skills}] of Object.entries(users)) {
if (skills.length > bestUserSkills) {
bestUserName = name;
bestUserSkills = skills.length;
}
}
console.log(bestUserName);
const bestUser = users[bestUserName];
console.log(bestUser);