Tuesday, 15 February 2011

Javascript ... Displaying Values -



Javascript ... Displaying Values -

i'm creating blackjack card game in javascript. appreciate comments far. i'm looking feedback why can seem phone call cardface function 1 time within carddealer function. @ nowadays cardface(0) gives me value whereas cardface(1) or other number not unless cardface(x) beingness called within carddealer function. ideas appreciated.

code below not finish believe plenty discern plenty info from.

function card(name, suit, face, value) { this.name = name; this.suit = suit; this.face = face; this.value = value; } aceofhearts = new card("aceofhearts","hearts", "ace", 11); kingofhearts = new card("kingofhearts","hearts", "king", 10); queenofhearts = new card("queenofhearts","hearts", "queen", 10); jackofhearts = new card("jackofhearts","hearts", "jack", 10); tenofhearts = new card("tenofhearts","hearts", "ten", 10); nineofhearts = new card("nineofhearts","hearts", "nine", 9); eightofhearts = new card("eightofhearts","hearts", "eight", 8); sevenofhearts = new card("sevenofhearts","hearts", "seven", 7); sixofhearts = new card("sixofhearts","hearts", "six", 6); var deck; deck = []; deck.push(aceofhearts); deck.push(aceofdiamonds); deck.push(aceofclubs); deck.push(aceofspades); deck.push(kingofclubs); deck.push(kingofdiamonds); deck.push(kingofhearts); deck.push(kingofspades); deck.push(queenofclubs); deck.push(queenofdiamonds); deck.push(queenofhearts); deck.push(queenofspades); deck.push(jackofclubs); deck.push(jackofdiamonds); deck.push(jackofhearts); deck.push(jackofspades); deck.push(tenofclubs); deck.push(tenofdiamonds); deck.push(tenofhearts); deck.push(tenofspades); var carddealer = function() { fisheryates(deck); document.getelementbyid("yourfirstcard").textcontent = "your first card " + cardface(0) ; document.getelementbyid("yoursecondcard").textcontent = "your sec card " + cardface(1) ; }; var cardface = function(x) { cardface = deck[x].face; homecoming cardface; }; var cardsuit = function(x) { cardsuit = deck[x].suit; homecoming cardsuit; }; <body> <div>black jack 1.1</div> <br/> <button type="button" onclick="carddealer()">deal</button> <div id="yourfirstcard"></div> <div id="yoursecondcard"></div> <div id="yourthirdcard"></div> <div id="yourfourthcard"></div> <div id="yourfifthcard"></div> <div id="playertotal"></div></div> <div id="playersit"></div> <div id="playermessage"></div> <div id="sitter"><button type="button" onclick="sit()">sit</button></div> <div id="hitter"><button type="button" onclick="hitme()">hit</button></div> <br/> <br/> <div id="dealersfirstcard"></div></div> <div id="dealerssecondcard"></div></div> <div id="dealersthirdcard"></div></div> <div id="dealersfourthcard"></div></div> <div id="dealersfifthcard"></div></div> <div id="dealertotal"></div></div> <div id="dealersit"></div></div> <div id="dealermessage"></div></div> </body>

here see issue:

var cardface = function(x) { cardface = deck[x].face; homecoming cardface; };

at first, declare variable called cardface , assign function. function reassigns cardface variable , returns result. that's it, function gone now, cannot refer anymore, since cardface has been reassigned.

what need alter body of function way doesn't reassign variable:

var cardface = function(x) { var cardfacevalue = deck[x].face; homecoming cardfacevalue; };

the same goes cardsuit function.

javascript

No comments:

Post a Comment