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 Programming Multiple Outcomes

oliverchou
oliverchou
20,886 Points

Only get 0 correct!!?

This is my code and it seems not working! Anyone can help me?

//initialization var correct = 0;

//ask Qs var answer1 = prompt("Name a programming language that's also a gem"); if (ans1.toUpperCase === 'RUBY') { correct += 1; }

var ans2 = prompt("Name a programming language that's also a snake"); if (ans2.toUpperCase === 'PYTHON') { correct += 1; }

var ans3 = prompt("What language do you use to syle web pages?"); if (ans3.toUpperCase === 'CSS') { correct += 1; }

var ans4 = prompt("What language do you use to build the structure of web pages?"); if (ans4.toUpperCase === 'HTML') { correct += 1; }

var ans5 = prompt("What language do you use to add interactivity to a web page?"); if (ans5.toUpperCase === 'JAVASCRIPT') { correct += 1; }

//output result document.write("<p>You got " + correct + " out of 5 questions correct.<p>");

//output rank if (correct === 5) { document.write("<p><strong>You earned a gold crown!</strong></p>"); } else if (correct >= 3) { document.write("<p><strong>You earned a silver crown.</strong></p>"); }

2 Answers

Daniel Gauthier
Daniel Gauthier
15,000 Points

Hey oliverchou,

I threw your code into Brackets and worked it out to this:

            //initialization 
            var correct = 0;

            //ask Qs 

            var ans1 = prompt("Name a programming language that's also a gem");
            if (ans1.toUpperCase() === 'RUBY') { correct += 1; }

            var ans2 = prompt("Name a programming language that's also a snake");
            if (ans2.toUpperCase() === 'PYTHON') { correct += 1; }

            var ans3 = prompt("What language do you use to syle web pages?");
            if (ans3.toUpperCase() === 'CSS') { correct += 1; }

            var ans4 = prompt("What language do you use to build the structure of web pages?");
            if (ans4.toUpperCase() === 'HTML') { correct += 1; }

            var ans5 = prompt("What language do you use to add interactivity to a web page?");
            if (ans5.toUpperCase() === 'JAVASCRIPT') { correct += 1; }

            //output result 
            document.write("<p>You got " + correct + " out of 5 questions correct.<p>");

            //output rank 
            if (correct === 5) { document.write("<p><strong>You earned a gold crown!</strong></p>"); } else if (correct >= 3) { document.write("<p><strong>You earned a silver crown.</strong></p>"); }

There were a few problems in the original code.

The first issue was that you declared your first variable as answer1, but tried to refer to it as ans1 in the first if block.

The second issue was that all of your calls to .toUpperCase were missing their brackets ().

After those two things were corrected, the code ran as intended.

Good luck with the course!

Daniel Gauthier
Daniel Gauthier
15,000 Points

Also going to leave this here, since it will help you if you ever need to post your code in the forums again:

How to Post Code on the Forums

There are two ways to share your code on the forums here, excluding using external tools from outside of Treehouse.

Method One

The first method is to use a series of three ` (backticks, located at the top left of the keyboard) without any spaces on one line, paste all of your code starting on the second line, then closing the code block with a second series of three backticks. Take a peek at the link for the "Markdown Cheatsheet" located above the "Post Answer" button anytime you're about to post a comment or answer. Using this method, the code will look like this:

```css
(code here)
```

Method 2

The second method is a little more convoluted, but it lets us look at your entire project and make our own copy to try fixing issues. I'll order the steps since it can be a little confusing the first time around:

  1. Open the workspace you're having trouble with.

  2. On the menu bar located to the left of the "Preview Workspace" button is the "Snapshot Workspace" (camera icon), click that.

  3. A popout will appear with another button that says "Take Snapshot", click that.

  4. It should create a new snapshot, which will appear beneath the "Take Snapshot" button in the popout. Click the snapshot you want to share.

  5. The snapshot should open a new browser tab. Highlight the url in the snapshot's browser tab (it should look like this: https://w.trhou.se/duckyj79b1i0 ).

  6. Create your forum post and paste the snapshot's url into the post. Other Treehouse users will be able to open the snapshot, then 'fork' a new copy to check out your code.

Keep in mind that you can only ever have five snapshots, but you can delete them by hovering over the snapshot located below the "Take Snapshot" button in the popout and clicking the trash bin icon.

Good luck!

oliverchou
oliverchou
20,886 Points

thank you~ I'll try it next time! :)