let players={
soccer:['messi','ronaldo'],
basketball:{lakers:['lebron','davis'],
rockets:['harden','bob']},
football: {rams:['obj','cooks'],
cheifs:'mahomes'},
hockey:{kings:['kopitar','brown'],
flames:['bob','john']},
};
function nameTheTeam(player){
for(var d=0;d<players.length;d++)
for(var i=0;i<players[d].length;i++){
if(player===players[d][i]){
return players[d][i];
} else {return "Not a player";}
}
}
console.log(nameTheTeam('curry'));
Can you explain a bit more about what you are trying to do and what is happening?
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>
) to add backticks around text.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).
That code makes no sense whatsoever. You try to loop over indices on an object that doesn’t have indices… also your first for-loop has no {}
.
Great question, I see what you are trying to do! Such a treat to read your code.
JS doesn’t allow looping over Objects with a For Loop unless the object key is an Array. As your code is, you could use your parent for loop to access each parent key in your players object that is an Array. Nested arrays could bypass the complexity of your players object key value that currently has a mixture of array as in players.soccer and objects as in players.basketball
Thank you so much for the amazing response. Is it possible for you to elaborate more on how I should accomplish my task correctly.
It would help if you describe what you are trying to do and what parts aren’t working.
We aren’t a soda pop machine of code. We need some more info!
Soda Pop Machine of Code, I like that.
Honestly, the task is like 3 levels ahead of your skills. You don’t even know on how to properly deal with objects - yet you have a complex structure with a variety of different depths and combinations of datastructures.
In order to this task, you might very well need a recursion which checks the datatypes and branches into different approaches depending on what it encounters.
Your right, his code needs curlies on the first For Loop and his next For Loop needs to be nested inside.
read and run this code and let me know what you think. My code works but is only a hint to solving your problem in one way.
function nameTheTeam(player){
for(var d=0;d<players.soccer.length;d++){
if(player===players[d]) return players[d][i];
return "Not a player";
}
}
nameTheTeam('lebron')
If you need to loop an object you have a few choices. For example, Object.entries() (or .keys()/.values() look at MDN) or you can also use a for…in loop.
Make sure the data structure is appropriate. Consider your data structure from an access point of view - i.e. think about how you want to access the data and what is most convenient.
Make sure the function is supplied with enough information, i.e. just a name is harder than a sport, team, and player name. If you made an app where people can search for a player name it isn’t unreasonable to have categories that let you drill down. So you start by picking the sport, then the team, and then the player name.
But as said, without knowing what the overall goal with the code is we can’t give you any advice on how to potentially change your data structure and how you access it.
In addition to correcting your syntax, you could simply model all your player object key values after your first key value, by making all your player keys Arrays of the same length. You can have objects as your Array elements where you see fit. Then since all your player object keys will be arrays of the same length you will get a value other than undefined(a string, object or whatever the element is within the Array passed as the player object key value.
Thanks for sharing that resource about how to loop objects. I wanted to look it up and glad you helped me find out about it sooner.
let object = ['soccer', 'basketball.lakers','basketball.rockets','football.rams','football.cheifs','hockey.kings','hockey.flames']
let players={
soccer:['messi','ronaldo'],
basketball:{lakers:['lebron','davis'],
rockets:['harden','bob']},
football: {rams:['obj','cooks'],
cheifs:'mahomes'},
hockey:{kings:['kopitar','brown'],
flames:['bob','john']},
};
function nameTheTeam(player){
for(var d=0;d<object.length;d++)
for(var i=0;i<object[d].length;i++){
if(player===players[object[d]][i]){
return players[d][i];
} else {return 'Not a player';}
}
}
console.log(nameTheTeam('lebron'));
why is it returning not a player?
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>
) to add backticks around text.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).
and it’s switched to return players[object[d]][i];
why can’t you use the loop for an object if its just raising the number every loop and then calling that property in the object. The loop is not connected to the object.
I think you need to learn some fundamentals about objects and arrays.
Where do you suggest I learn that? I already went through a lot of the java course on FCC.
How much is a lot? Your code is communicating lot of misunderstanding about accessing object properties and array elementl.