I'lldoittomorrow I'lldoittomorrow - 1 month ago 8
jQuery Question

Why is the rest of my JS code not letting my JQuery run?

apologies for the basic question but I'm just starting to learn JS. I'm trying to build a blackjack game, and I started off in my .js file by creating a constructor function (for the cards) and then I defined one of the methods in that function. Here is the code in my .js file (I've not shown what's inside the actual cardToString function as it's about 70 lines long):
EDIT: Some people have asked I include the entire code, so here it is:

function Card(rank, suit) {
this.rank = rank;
this.suit = suit;
this.toString = cardToString;
this.createNode = cardCreateNode;
}
function cardToString() {
var rank;
var suit;
switch (this.rank) {
case "A" :
rank = "Ace";
break;
case "2" :
rank = "Two";
break;
case "3" :
rank = "Three";
break;
case "4" :
rank = "Four";
break;
case "5" :
rank = "Five";
break;
case "6" :
rank = "Six";
break;
case "7" :
rank = "Seven";
break;
case "8" :
rank = "Eight";
break;
case "9" :
rank = "Nine";
break;
case "10" :
rank = "Ten";
break;
case "J" :
rank = "Jack";
break;
case "Q" :
rank = "Queen";
break;
case "K" :
rank = "King";
break;
default :
rank = null;
break;
}
switch (this.suit) {
case "C" :
suit = "Clubs";
break;
case "D" :
suit = "Diamonds";
break;
case "H" :
suit = "Hearts";
break;
case "S" :
suit = "Spades";
break;
default :
suit = nill;
break;
}

if (rank == null || suit == null)
return "";
else
return rank + " of " suit;
}

$(document).ready(function() {
$("#deal").click(function() {
$("#hit").fadeOut('slow');
});

});


I'm completely confused, because when I put this into the .js file the jQuery doesn't work, however when I comment out everything but the jQuery it works fine. I'm sure it's something basic that I'm just not aware of, but I've searched for a while and can't find an answer. Basically, I have no idea why my first few bits of JS stop the jQuery from working.

EDIT: Someone asked me if I checked the console for errors, and sure enough I do get one:

Uncaught SyntaxError: Unexpected identifier

Next to this it said "jsjquery.js:4" and I think 4 refers to the line of code which was an empty line right at the beginning (I have some comments on the first few lines, then left a line before I starting coding). I removed empty line, now it says the error is on line 80, which is this piece of code:

return rank + " of " suit;


This error goes away when I comment out everything but the jQuery.
This might make everything clear to you guys, but I'm still lost!

Thanks.

EDIT: Ok final edit I promise, haha. For anyone who might come across this question looking for an answer: I made a couple of syntax mistakes, but the real problem was that I didn't define the method cardCreateNode. I didn't realise that not doing so would cause everything else to not run. Thank you to everyone who responded!

Answer

Since you have a different error based on the update of your post I'll answer your latest issue being that you receive an error on this line:

return rank + " of " suit;

You are missing the concatenation on the other side of " of " so it should look like this:

return rank + " of " + suit;

Also in your switch statement you have this line:

default :
    suit = nill;
    break;

I think here you mean to set the value to null not nill which would look like this:

default :
    suit = null;
    break;