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 trialJuan Prieto
976 PointsI don't understand what it says return "as an expression"
I think I'm solving the problem, but I don't know how to resolve it as an expression. Also, What is the meaning of expression in coding?
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) {
tiles += tile;
}
public boolean hasTile(char tile) {
boolean result = tiles.indexOf(tile) != -1;
if (result == true){
return true ;
}
else; {
return false;
}
}
}
1 Answer
michaelcodes
5,604 PointsHi there! an expression is basically a combination of variables, method calls, and\or logical operators. It is a very broad term. In the case of this example you have the expression here:
tiles.indexOf(tile) != -1;
This is a boolean expression that will return either true or false, you were correct in assigning it to a boolean. However you dont need to then use an if statement to check if its true or false since the boolean handles that, we can just return the boolean variable result directly as such:
public boolean hasTile(char tile) {
boolean result = tiles.indexOf(tile) != -1;
return result ;
}
Since the method is declared as a boolean returning method, it is expecting to be returned a boolean, since it already knows this we can make the code cleaner and shorter by returning directly and not making a separate boolean variable:
public boolean hasTile(char tile) {
return tiles.indexOf(tile) != -1;
}
Hope that helps!