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 trialHilary Katsande
9,045 PointsAdd a conditional statment that opens an alert dialog box
Add a conditional statement that opens an alert dialog box when answer is the string
var answer=prompt('What is the best programming language);
if(answer==='you are correct') {opens an alert dialog box
}
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JavaScript Basics</title>
</head>
<body>
<script src="app.js"></script>
</body>
</html>
2 Answers
Robert Anthony
19,952 PointsYou have to check whether the string is equal to 'JavaScript' and alert 'You are correct' if it is.
So, you need an if statement:
if (answer === 'JavaScript') {
alert ('You are correct');
}
Steve Hunter
57,712 PointsHi there,
You haven't closed the string in the first line; add a '
after the word 'language' and before the bracket.
Next, you want to see if the answer
is 'JavaScript'. If it is, use the alert()
method to prompt the user with the message 'You are correct'. Pass that string into the alert()
method by putting it inside the brackets.
Steve.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsYou need to close the string inside the
alert()
method.And you need to use
===
triple equals for the comparison.Steve.
Robert Anthony
19,952 PointsRobert Anthony
19,952 PointsI agree about the missing ' doh! But the == will work as well as they are both strings.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsAgree in principle but in this context, i.e. passing the challenge, we need to use triple equals. It's part of the challenge tests.
Robert Anthony
19,952 PointsRobert Anthony
19,952 PointsOK, understood. Updated. Thanks for the feedback.
Steve Hunter
57,712 PointsSteve Hunter
57,712 Points