Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAmon Dow III
4,086 PointsI need help I think I am a little confused on exactly what they are asking need a little assistance
String[] sports = { "Basketball", "Baseball", "Tennis" }; String bestSport; sports=sports[bestSport];
String[] sports = { "Basketball", "Baseball", "Tennis" };
String bestSport;
sports=sports[bestSport];
3 Answers
Nejc Vukovic
Full Stack JavaScript Techdegree Graduate 51,574 PointsString[] sports = { "Basketball", "Baseball", "Tennis" }; // declaring sports as an array with those string variables
String bestSport; // declaring a string with var name bestSport which evaluates to undefined
sports=sports[bestSport]; // assigning value sports[bestSport] to the variable sports = evaluates to undefined
I'm not sure what are you trying to do (I'm not that far yet), but your bestSport either needs a int value to get the variable out of the array or you do on the last line:
sports=sports[2]; //which assigns the string "Tennis" to your sport variable - but this overrides you array then
bestSport = sports[2]; //this would be the right answer if you are trying to take an element from the array and set it to the new variable you declared.
Give me more info so I could help you precisely. But I hope this answer is helpful.
Regards,
Nejc
John Amadeo
5,546 PointsI assume the question is "Declare a String variable named bestSport and initialize it to the first element of the sports array." ?
Basically the question is asking you to create a new variable named 'bestSport' whose value is equal to the first item of the 'sports' array.
In general, this would essentially look like:
String bestSport = sports[x];
where x is the nth element in the array.
Thus, if you want get the first element, the code below the array should be
String[] sports = { "Basketball", "Baseball", "Tennis" };
String bestSport = sports[0];
Hope it helps!
Amon Dow III
4,086 PointsThank you, I understand now. I did not understand the task. Now, I completely understand great explanation.
Arne Ellenberg
2,353 PointsYou have to set the String bestSport equal to the first element of the array:
String bestSport = sports[0];
Amon Dow III
4,086 PointsAmon Dow III
4,086 PointsI follow what your saying, but I misunderstand the assignment. Thanks