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 trial
Trevor Johnson
14,427 PointsQuestion on if else & concatenation
So I just solved this challenge on FCC, but I have a question about why my first solution wouldn't work, and this one did. The challenge was to create a card counting machine that would add up the points of each card, and then to add either "Bet" or "Hold", depending on the total outcome.
var count = 0;
function cc(card) {
// Only change code below this line
switch(card) {
case 2:
case 3:
case 4:
case 5:
case 6:
count += 1;
break;
case 7:
case 8:
case 9:
count += 0;
break;
case 10:
case "J":
case "Q":
case "K":
case "A":
count -= 1;
break;
}
return count + (count > 0 ? " Bet" : " Hold");
// Only change code above this line
}
// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(3); cc(7); cc('K'); cc('A');
This is the correct code, but what I originally tried was this:
if (count > 0) {
return count += "Bet";
} else {
return count += "Hold";
}
The only reason I got the solution was because I found it somewhere else So I am just looking for some explanation. I also don't even understand what the return solution really is because I have never seen that before.
2 Answers
james south
Front End Web Development Techdegree Graduate 33,271 Pointsthis line:
return count + (count > 0 ? " Bet" : " Hold");
utilizes the ternary operator. the syntax is (conditional statement ? answer if true : answer if false) so the return statement starts with the count variable, then asks, is count greater than 0? if it is, Bet is added, if not, Hold is added. thus it is equivalent shorthand for the longer traditional if else statement.
Seth Kroger
56,416 Pointscount is declared in the global scope, so changes to it inside the function stick around outside of it as well. The main difference between the two solutions is that the "correct" solution uses return count += with the return statement which modifies count as well as returning the value. Your solution uses return count + which returns the value of the expression, but doesn't change the value of count.
Trevor Johnson
14,427 PointsIn the if else code that I originally tried I did return count +="bet" and return count +="hold" but it didn't work. My thinking was that if count < 0 then "hold" would be added to the total, and the total + hold would be returned. And if count > 0 then "Bet" would be added to the total and total + bet would be returned. I still don't get why that didn't work. You said that the "correct" answer uses return count += and that is what I did.