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 trialTim Huurdemam
Courses Plus Student 323 PointsI'm stuck
I don't know how to do it, I can't come up with the answer. Does anybody know how to do it? The question was: Create a new String variable named bestSport and set its value it to be the first element of the sports array.
String[] sports = { "Basketball", "Baseball", "Tennis" };
String bestSport = sports(0);
3 Answers
Michael Salomon
1,356 Pointsuse [] instead of {};
String[] sports = { "Basketball", "Baseball", "Tennis" }; String bestSport = sports[0];
Yael Wells
15,979 PointsWhen you want to get an element from an array knowing the position of the element, you have to put the position in "[]".
For example:
String[] mobileOS = ["Android", "IOS"];
int position = 0;
String myOS = mobileOS[position];
"()" is commonly used to call a function.
Joe White
9,750 PointsIf I remember correctly, I had to do this with 3 lines of code for it to accept the result
Tim Huurdemam
Courses Plus Student 323 Pointswhat can I add to make the three lines?
Joe White
9,750 PointsAs per Yael' answer