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 trialMichael Stedman
Full Stack JavaScript Techdegree Student 13,838 PointsCompletely stumped on second objective of the first Code Challenge of Creating the MVP course. Any help?
No matter what I do, I cannot get passed the second half of this particular code challenge. A couple of things that I have tried so far gives me the "Bummer" red X and says there are several ways to solve the problem, but it wants me to do something specific. Can someone post up what it is that they typed for the answer that allowed them to pass the second part of the Code Challenge and move on with credit for it?
Below I will quote the question/instruction, and below that I'll post the code that I currently have (which is just the code the challenge provides with the exception of what I completed in the "TODO" for the addTile
method which was part of objective 1 for the Code Challenge)
"Correct the existing hasTile
method to return true
if the tile is in the tiles field, and false
if it isn't. You can solve this a few ways, however, I'd like you to practice returning the result of the expression that is uses the index of a char in a String."
public class ScrabblePlayer {
// A String representing all of the tiles that this player has
private String tiles;
public ScrabblePlayer() {
tiles = "";
}
public String getTiles() {
return tiles;
}
public void addTile(char tile) {
// TODO: Add the tile to tiles
tiles += tile;
}
public boolean hasTile(char tile) {
// TODO: Determine if user has the tile passed in
return false;
}
}
public class ScrabblePlayer {
// A String representing all of the tiles that this player has
private String tiles;
public ScrabblePlayer() {
tiles = "";
}
public String getTiles() {
return tiles;
}
public void addTile(char tile) {
// TODO: Add the tile to tiles
tiles += tile;
}
public boolean hasTile(char tile) {
// TODO: Determine if user has the tile passed in
return false;
}
}
8 Answers
Michael Stedman
Full Stack JavaScript Techdegree Student 13,838 PointsSo.... Finally figured it out and answered it myself. Utilizing the corrected typo in the instruction sentence, and the several comments from Craig Dennis I was able to fiddle around and get the answer. Literally, it was just a bunch of trial and error and piecing things together from the comments Craig made. I'm going to go ahead and paste the code that I got that allowed me to pass the second objective as ONLY after looking at it (after I knew it passed) did I actually understand how it was what Craig was talking about. Finally getting to see it is what gave me the opportunity to understand it as asking on here I have to wait for answers to pop up, and then some are not always what the exact answer needed. Thanks for a tough one, Craig Dennis!
public void addTile(char tile) {
// TODO: Add the tile to tiles
tiles += tile;
}
public boolean hasTile(char tile) {
// TODO: Determine if user has the tile passed in
return tiles.indexOf(tile) >= 0;
}
Fabian Pijpers
Courses Plus Student 41,372 PointsThanks, Michael I was getting close but missed out. The problem i had was trying to throw an if else blok around it. The solution is much easier.
Sean McLImans
4,559 PointsThank you. I was stumped as well. I was trying to do an if else statement. Another possible answer is:
public boolean hasTile(char tile) { // TODO: Determine if user has the tile passed in return tiles.indexOf(tile) != -1; }
Junjie Huang
4,704 PointsTake a look at indexOf method: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)
If no such value of k exists, then -1 is returned.
harvey Spectre
Courses Plus Student 1,423 Pointspublic class ScrabblePlayer { // A String representing all of the tiles that this player has private String tiles;
public ScrabblePlayer() { tiles = ""; }
public String getTiles() { return tiles; }
public void addTile(char tile) { // TODO: Add the tile to tiles tiles+=tile;
}
public boolean hasTile(char tile) {
return tiles.indexOf(tile) > -1;
}
}
aroshinemunasinghe
5,649 PointsHi thanks for sharing you thought Here is what I have been tried to solved out his quiz. He wrote " *if the tiles is in the ..." So I thought aha just if states but then got a bummer even thought I got same answers like you got tiles.indexOf(tile) >= 0
```Java
public boolean hasTile(char tile) {
// TODO: Determine if user has the tile passed in
/*
return true if the tiles is in the tiles files, and false if it isnt. you can solve this a few ways
however I like to practise returning the result of the expression that uses thee index of a char in a string.
*/
if (tiles.indexOf(tile) >= 0) {
return true;
} else {
return false;
}
}
```
But got a bummer again: Bummer! While you could definitely solve this using an if statement, try returning the result of the expression.
" Try to returning the result of the expression" ahha ? no if states then just return. But ?? Why? In logically it is correct, it is OK to write this way, I could get passed
Muhammad Asif
Courses Plus Student 557 Pointspublic class ScrabblePlayer {
// A String representing all of the tiles that this player has private String tiles;
public ScrabblePlayer() { tiles = "";
}
public String getTiles() { return tiles; }
public void addTile(char tile) { // TODO: Add the tile to tiles tiles+=tile;
}
public boolean hasTile(char tile) { // TODO: Determine if user has the tile passed in
return tiles.indexOf(tile) >= 0;
} }
Fabian Pijpers
Courses Plus Student 41,372 Pointspublic boolean hasTile(char tile) {
// TODO: Determine if user has the tile passed in
if (titles.indexOf(tile) >= 0)
{
hasTile = true;
}
else{
hasTile = false
}
}
Michael Stedman
Full Stack JavaScript Techdegree Student 13,838 PointsMichael Stedman
Full Stack JavaScript Techdegree Student 13,838 PointsI guess somehow it pasted in the ScrabblePlayer.java code which is the same that I copy/pasted.