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 trialAgnes Caringal
6,239 Pointsits all about " || and &&" conditions, whats wrong with this code?
guys..can you help me and explain this code... Thanks
var money = 9;
var today = 'Friday'
if ( money >= 100 || today === 'Friday' ) {
alert("Time to go to the theater");
} else if ( money >= 50 || today === 'Friday' ) {
alert("Time for a movie and dinner");
} else if ( money > 10 || today === 'Friday' ) {
alert("Time for a movie");
} else if ( today !== 'Friday' ) {
alert("It's Friday, but I don't have enough money to go out");
} else {
alert("This isn't Friday. I need to stay home.");
}
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JavaScript Basics</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
2 Answers
Marcus Parsons
15,719 PointsHey Agnes,
I'm sure you know by now that || is like saying "or" in JavaScript. We want the script to output in an alert "It's Friday, but I don't have enough money to go out". The first thing we should do is look at the if statements. Right now they say "If money is greater than or equal to 100 or today is Friday", then show the alert for that if statement. That means that the if-statement will execute if either of those conditions is true. That wouldn't get what we want to show up. Instead, we'd want to use && (and) logic, so that the if statement only executes if both statements are true.
Follow that pattern all the way down to the last else if statement, and you see that it says "If today is not Friday" show the alert. But the alert mentions that it is Friday. That means we need to see "if today is Friday", then show the alert, so change the !== to ===. This last else if statement will be executed because the money is equal to 10 or less (even though we didn't explicitly say that) and the day is "Friday".
var money = 9;
var today = 'Friday'
//changed all || to && logic
if ( money >= 100 && today === 'Friday' ) {
alert("Time to go to the theater");
} else if ( money >= 50 && today === 'Friday' ) {
alert("Time for a movie and dinner");
} else if ( money > 10 && today === 'Friday' ) {
alert("Time for a movie");
//changed the !== to ===
} else if ( today === 'Friday' ) {
alert("It's Friday, but I don't have enough money to go out");
} else {
alert("This isn't Friday. I need to stay home.");
}
Agnes Caringal
6,239 Pointsok, I will try now Marcus :) how long you been in web development world? thanks a lot for the info.
Marcus Parsons
15,719 PointsI've been interested in web development for several years now, but I didn't start getting serious about it until about a year and a half ago. I sharpen my skills by helping other people out, and by working on JavaScript projects I've created or that I started here on Treehouse. One example I'm proud to show off is my program SketchMeh. It was built off of the "Simple Drawing App" here on Treehouse. But, I added quite a few extra tools, extra functionality such as Undo (Ctrl + Z) and Redo (Ctrl + Y), etc.
Just think of something random you'd like to do and go for it. It'll be frustrating at times, but when you figure your problems out, you'll be much better acquainted with JavaScript, HTML, and CSS. =]
Agnes Caringal
6,239 PointsAgnes Caringal
6,239 Pointsthank you so much Marcus Parsons. Can you give me some tips or sites that can help me a lot about Javascript.
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsOne site I always recommend is Codeacademy.com. I love their courses because they are very interactive and they're all free. You're welcome, Agnes. =]