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 The Conditional Challenge

Jeriah Bowers
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
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
231,271 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
231,271 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!!");
}