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

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements Introducing Conditional Statements

David Dassau
David Dassau
8,628 Points

Not sure why my code isn't working...

It seems to me like this should be the correct answer to this part of the challenge, but apparently I'm doing something incorrect.

app.js
var answer = prompt("What is the best programming language?");
if ( answer() === 'JavaScript') {
  alert("You are correct");
}
index.html
<!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>

4 Answers

Hi David,

As Chyno said, a minor syntax error. Just take out the parentheses on answer inside of your conditional statement, like so:

if ( answer === 'JavaScript')

The answer variable is storing the data returned from the prompt() as a string, but it isn't a function itself. When you use parentheses like this: answer(), JavaScript looks for a function, but can't find one with that name in your code, so it returns an error.

I am not sure which task you are on but you need to remove the () from answer().

Just echoing everyone else here. In your conditional, I am assuming you are wanting to check to see what the result of the prompt is, which you have stored in var answer. Your issue is that you are checking answer() which is checking a function. Your variable answer is not a function, it is simply storing the value of the prompt.

If you try to run your current code in Chrome, open up the console and you should receive an error with something along the lines of "answer is not a function"

To correct this, as others have stated, simply remove the () from the answer() check in your conditional.

SIMPLE SYNTAX ERROR HERE, no biggie :)

Hi David,

if ( answer() === 'JavaScript') 

Should be

if ( answer === 'JavaScript').

We want to get the value stored in answer, so we can just reference the variable name. Using () will 'call it'.

Example:

var answer = prompt("What is the best programming language?");

answer; // will give us the value entered into the prompt by user
answer(); // will give an error, as it attempts to call a function assigned to answer. 

Hope this helps :)