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
Ryan B.
3,236 PointsJavaScript '&&' '||' operators exercise.
Hello everyone! I'm working on another program and learning how to use the ampersand(&&) and or(||) operators. They have us building out a story with cases then they want us to go back and add our conditionals using these operators. I can really use the help. The instructions are below followed by my code. If you can help me figure this out I can finally move on to the next lesson. Unfortunately, their forum is not as engaged as Treehouse. So, my question is where do I add the if/then statement using the && || operators? What am I comparing exactly?
Instructions:
Add some if/else statements to your cases that check to see whether one condition and another condition are true, as well as whether one condition or another condition are true. Use && and || at least one time each.
Code:
var user=prompt("You're at a baseball game. You catch a rookies first major league home run. Do you give it back?").toUpperCase();
switch(user){
case 'Throw it back!':
console.log('You must not be a fan!');
break;
case 'You keep it in exchange for money!':
console.log("Wow! Leverage at its best!");
break;
case 'You give it back in exchange for an autograph':
console.log('You are a true fan!');
break;
default:
console.log('Do you even know who he is?');
}
Ryan B.
3,236 PointsI need to add If/else statements using the operators mentioned above so the user can play out the options provided in each 'case'.
6 Answers
Jonathan Leon
18,813 PointsFirst, your code marked
var user=prompt("You're at a baseball game. You catch a rookies first major league home run. Do you give it back?").toUpperCase();
switch(user){
case 'Throw it back!':
console.log('You must not be a fan!');
break;
case 'You keep it in exchange for money!':
console.log("Wow! Leverage at its best!");
break;
case 'You give it back in exchange for an autograph':
console.log('You are a true fan!');
break;
default:
console.log('Do you even know who he is?');
}
Okay.
Try to follow this logic.
if \ if else \ while conditions take a boolean as a paramater (inside here).
boolean is either true; or false;
when it's (true), the code block { here } will run. else the code block after the word else {here} will run.
while(boolean) will run {this code} as long as the boolean is true.
The operators combine that boolean logic into one. for example
the AND operator && requires all of the boolean values to be true
(1+2==3) true; (1+2==3 && 1+1==2) true ; only because both booleans are true, but (1+2==3 && 1+1==3) false; because only the first boolean is true
the OR operator ||
(1+2==3) true; (1+2==3 && 1+1==2) true ; because either of them is true; (1+2==3 && 1+1==3) true; because one of them is true; (1+2==4 && 1+1==3) false; because neither of them is true;
So for example;
if (true && false) {
//code won't happen
} else if (true || false) {
//code will happen
}
Ryan B.
3,236 PointsHey Jonathan thank you for taking the time to help, much appreciated. I completely understand how they work i'm just trying to figure out how to place them in my code. What would my first else/if statement look like? So... the prompt asks whether or not you will return the home run ball. If YES you have the option to throw it back which makes you not a fan. If NO, you can exchange it with the athlete for money or an autograph. If you exchange it for money, you are leveraging the ball(kind of a bold move lol) but if you return it for an autograph, you're a true fan fan of the athlete. For some reason I'm confusing myself :*(
Jonathan Leon
18,813 PointsOkay, I see the breaks happen after every decision so that will end that function whenever you chose one of them. so there are three options.
If user = "YES" - first case runs
else prompts user do you want to keep it to yourself?
if user = "YES" - second case runs
else third case runs
Is there anything else added to that code? Because I'm not really familiar with case switches, all I know is that each case switch breaks and returns so the only reason to use these conditions are to trigger these events, means you have to prompt at least two more times (for three options total) . Could you refer the site you're taking this lesson at please?
Jonathan Leon
18,813 PointsMaybe try to write down the story and it will make it simpler. I'm not a sports fan at all so I can't even imagine it in my head but I bet it's spectacular :3
Jonathan Leon
18,813 PointsMaybe try to write down the story and it will make it simpler. I'm not a sports fan at all so I can't even imagine it in my head but I bet it's spectacular :3
Jonathan Leon
18,813 PointsMaybe they want you to use the operators in order to add alternative answers and not just YES?
Ryan B.
3,236 PointsIt's CodeAcademy, Choose Your Own Adventure exercise 5 of 6. Yes they want me to add the if/else statements using the ampersand and || operators. Maybe I should simplify my story?
Jonathan Leon
18,813 PointsProbably. The only thing that jumps to my head from my learning here at treehouse about this code is DRY;
You repeated yourself three times with :Case, console.log and break
So maybe a good use of conditionals and operators would be to take that input, check it with if\else statements and then choose the relevant case \ log the relevant text and break(which is excatly the same)
What do you think? :3
Jonathan Leon
18,813 PointsProbably. The only thing that jumps to my head from my learning here at treehouse about this code is DRY;
You repeated yourself three times with :Case, console.log and break
So maybe a good use of conditionals and operators would be to take that input, check it with if\else statements and then choose the relevant case \ log the relevant text and break(which is excatly the same)
What do you think? :3
Ryan B.
3,236 PointsOk. So I was able to pass the challenge but unfortunately it goes directly to the default answer which is console.log("Do you even know who he is?");.... Here's what I did, maybe you can try to get it to run all options.
var user=prompt("You're at a baseball game. You catch a rookies first major league home run. Do you give it back?").toUpperCase();
switch(user){
case 'Throw it back!':
console.log('You must not be a fan!');
break;
case 'You keep it in exchange for money!':
var fan=prompt("Are You a baseball fan, YES or NO?").toUpperCase();
var team=prompt("Is he on your team YES or NO?").toUpperCase();
if(fan===NO && team===NO){
console.log("Wow! Leverage at its best!");
}else{
console.log("You give it away!");
}
break;
case 'You give it back in exchange for an autograph':
var pen=prompt("Do you have a pen, YES or NO?");
var name=prompt("Do you know his name, YES or NO?");
if (pen===YES || name===YES){
console.log('You are a fan!');
}else{
console.log("Give it up!");
}
break;
default:
console.log('Do you even know who he is?');
}
Dan Oswalt
23,438 PointsDan Oswalt
23,438 PointsWhat is your question about the how to use the operators?