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

What is problem with my codes

i can't able to find that why my drawing is not working?

// problem: no user interaction is available // solution: when user interact make appopriated changes.

console.log("var color = $(".selected").css("background-color");
var $canvas = $("canvas");
var context = $canvas[0].getContext("2d");
var lastEvent;
var mousedown = false;
// when clicking on controlr list item
$(".controls").on("click", "li", function(){

  // deselect sibling elements
  $(this).siblings().removeClass("selected");
  // select clicked element
  $(this).addClass("selected");
   // cache the color
  color =$(this).css("background-color");
})

// when newcolor is pressed

 $("#revealColorSelect").click(function(){
   // show color select or hide the select 
   changeColor();
   $("#colorSelect").toggle();
 })

//update the newcolor span
  function changeColor(){
    var r =$("#red").val();
    var g =$("#green").val();
    var b =$("#blue").val();
    $("#newColor").css("background-color" , "rgb("+ r + ", "+ g + ", "+ b +")");
  }


// when color sliders change 

 $("input[type=range]").change(changeColor);

// when the addcolor is pressed
 $("#addNewColor").click(function(){

   // append the color to controle ul
   var $newColor = $("<li></li>");
   $(".controls ul").append($newColor);
   $newColor.css("background-color", $("#newColor").css("background-color"));
   // select the new color
   $newColor.click();


 })


// on mouse events on the canvas
 $canvas.mousedown(function(e){
  lastEvent = e;
   mousedown = true;
 }).mousemove(function(e){
   lastEvent = e;
   if(mousedown) {
 context.beginPath();
 context.moveTo(lastEvent.offsetX, lastEvent.offsetY);
 context.lineTo(e.offsetX, e.offsetY);
 context.stroke();
 context.strokeStyle = color;
   lastEvent = e;
   }
}).mouseup(function(){
 mousedown = false;

 }).mouseleave(function(){
  $canvas.mouseup();

 })")

Hi ashok bishnoi, your code is pretty dense. If you could edit your post to wrap the code with three backticks (`) that would really improve the legibility so that you can get help with your problem.

If I type this:

```javascript
console.log("Use backticks to escape your code.");
```

it becomes:

console.log("Use backticks to escape your code.");

Ok..done, now please give me solution/

3 Answers

Most of your code looks okay to me, but the first line reads:

console.log("var color = $(".selected").css("background-color");

It looks like perhaps you were trying to print something to the console, perhaps the entire script (remember that Javascript can't have strings across multiple lines so that won't work). I'm looking at the orphaned quote on the last line and connecting it to the unmatched quote on the first line. The problem is that you cannot nest strings inside of other strings using the same type of quote without escaping the inner set of quotes. The parser has no way of knowing what to include in the outer quote and what is outside the quote.

There are two ways around this. First, you can escape the nested quotes with a backslash character (\). So your first line would be something like:

console.log("var color = $(\".selected\").css(\"background-color\");");

Or you can use a different quote to wrap the set of inner quotes; wrap double quotes in single quotes or wrap single quotes with double quotes. This would make the first line:

console.log('var color = $(".selected").css("background-color");');

This has the advantage of not having to escape every quote inside the string, but it also has limited applicability as it is not too hard to find you have a mix of quotes (in part because an apostrophe and a single quote are indistinguishable).

Finally, I would recommend the use of jshint.com to help you debug your code in situations like this. Most of what I did was just to paste your code in there and go through each warning one by one.

but when i edit your sggested code by Drawing App is still not working, nothing even i am not able to change color, draw lines,,,nothing,is there any big bug in codes.

What browser are you using? I ran into an issue when I was going through the drawing app lesson using Firefox. I dug into the documentation for the jQuery Event object and came across something in the .mousemove() documentation that says different browsers support properties like .offsetX to different degrees and that jQuery only normalizes .pageX and .pageY. These are a little difficult to use because their origin is the top left corner of the document and not the element that binds the handler, but at least I was able to get something.

My suggestion would be to either try replacing .offsetX and .offsetY with .pageX and .pageY, respectively, or to try running your code in Chrome, which seems to be the preferred browser of the Treehouse instructors anyway.

i got the error in console ==== Failed to load resource: the server responded with a status of 404 (Not Found)

What was the resource that failed to load?

i got error not on codes but on workspace link.

Failed to load resource: the server responded with a status of 404 (Not Found)

http://port-80-pqhj3xfhm8.treehouse-app.com/js/app.js

That means that the machine can't find your javascript file. You should check that the name of the file matches exactly what script tag in your index.html file indicates. Also make sure that you have your app.js file inside your js folder (and if not, drag it into that folder).

plz run my codes in your seprate workspace and if they work then give me reply, if they will inform me i will use them any other site & editor.