Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements The Conditional Challenge

Jeriah Bowers
seal-mask
Jeriah Bowers
Front End Web Development Techdegree Graduate 20,590 Points

else statement not running.

https://w.trhou.se/969t9rdalw Here is a link to my workspace. I coded somthignreally simple to show what's going wrong and see how to fix it. The else statement won't run i.e. if i type 6 as my answer it still pops up with "Yes!!". Any help is welcomed. Thank you.

Steven Parker
Steven Parker
221,297 Points

You can't share a direct URL to your workspace, it's temporary and only exists while you are using it.

But you can use the snapshot function in the workspace and provide the link to that.

1 Answer

Steven Parker
Steven Parker
221,297 Points

Here's the original code, with comments added:

prompt("What is 2 + 2?");   // this asks the question, but it doesn't save the reply
var answer = ("4");         // this sets the variable named "answer" to contain the string "4"
if (answer) {               // this checks the "truthiness" of the string "4"
  alert("Yes!!");           // all non-empty strings are "truthy"
} else {
  alert("No!!");
}

What you probably intended was something more like this:

var answer = prompt("What is 2 + 2?");  // save the reply in the variable "answer"
if (answer == "4") {                    // check if the answer is "4"
  alert("Yes!!");
} else {
  alert("No!!");
}