Tag Archives: tutorial

Adding the Methods

Step5: Adding methods to interpret actual suit and value of cards


//Hand Constructor

var Hand = function(){

    this.card1 = deal();


    console.log(“card1 is “+this.card1.number+” of ” + this.card1.suit);

    this.card2 = deal();

    console.log(“card2 is “+this.card2.number+” of ” + this.card2.suit);




//Card Constructor

var Card = function(s,n){

    this.number = n;

    this.suit = s;



            this.getSuit = function(s){

     //console.log(“I’m here in getSuit”);


        case 1:

            this.suit = “clubs”;


        case 2:

            this.suit = “diamonds”;


        case 3:

            this.suit = “hearts”;


        case 4:

            this.suit = “spades”;





            this.getValue = function(n){

     console.log(“I’m here in getValue”);

     if (n === 1){

         this.value = 11;


    else if (n >= 11){

        this.value = 10;



         this.value = n;






//Deal function

var deal = function(){

            var suit = Math.floor(Math.random()*4)+1;

            console.log(“deal function suit: “+suit);

var number = Math.floor(Math.random()*13)+1;

            console.log(“deal function number: “+number);

            return new Card(suit,number);



//Main Function

var myHand = new Hand();



console.log(“card1 is a “+myHand.card1.suit+” of value “+myHand.card1.value);



console.log(“card2 is a “+myHand.card2.suit+ ” of value “+myHand.card2.value);



In this section I added two methods (these are just functions that handle objects). Methods are placed inside of the objects they work on (is this right?Always?). In this program my methods interpret the suit and number variables as the suit that the number represents (clubs, diamonds, etc) and the value of the card (ace = 11, face cards are all worth 10 and numbered cards are worth their numerical value).

As usual, I have included a couple console.log statements so that I could track where problems cropped up along the way.



Tags: , , , , , ,

Objects and Methods made my head swim – but not any more!

I’ve really been struggling with understanding objects and methods from my work on codecademy. Although it is a great site, because it is crowdsourced teaching, some topics just don’t get the best treatment. It’s not all that common to haveImage someone who knows how to do something well and have them also be able to explain it well to a student (just ask some of my poor victim / students from my first several semesters teaching).

Nevertheless, I trolled through some of the Q&A sessions looking for a good explanation of the material I was struggling with and couldn’t find anything that answered my question very well. What I did find, though, were some decent pieces of ideas that I thought I could combine into a more comprehensive bolus of work that reviewed the topics I just covered, provided some practice working with objects and methods and even a switch statement.

Here’s the setup:

Shermer High school. A couple famous students go there and we can make up Objects representing each of these students using a constructor. Also, each of these students has a brand name jacket that they cherish and these are also created as objects. So the twist is: we’re creating objects(Persons) who have other objects(jackets) as one of their properties. Furthermore, to make this exercise include methods as well, I’ve added ‘wash’ and ‘sew’ methods. Each time one of these methods is carried out, the jackets are changed (e.g. when you wash a jacket, you lose a button. When you sew a jacket, you add a button).

What I wanted to accomplish was to make these objects and methods and learn how to call them so that they carry out their work. Presented below is this program with some annotation and console.log statements that should clear up what’s going on.

Please let me know if this is helpful to anyone – or if this could be cleaned up and done in a more clear and concise way.

// Constructor to make jackets
function Jacket(brand){
case jordache:
this.make = “Jordache”;
this.buttons = 5;
this.color = “blue”;

case gap:
this.make = “Gap”;
this.buttons = 2;
this.color = “brown”;

case oldNavy:
this.make = “Old Navy”;
this.buttons = 3;
this.color = “white”;


//constructor to make people
function Person(name,age,brand) { = name; //unique
this.age = age; //unique
this.brand = new Jacket(brand); //object = “Shermer High”;    //constant
this.sayHello = function(){
console.log(“Say Hello to ””, student at “;
//method that washes jacket – loses a button each time
this.wash = function(){
console.log(“You just washed “ +”‘s Jacket.”);
this.brand.buttons = this.brand.buttons-1;
console.log(“That jacket now has “+this.brand.buttons+” buttons.”);
return this.brand.buttons;
}; = function(){
console.log(“You just mended “”‘s jacket.”);
this.brand.buttons = this.brand.buttons +1;
console.log(“That jacket now has “+this.brand.buttons+” buttons.”);


// Make three people using a constructor
var geek = new Person(“Anthony Michael Hall”, 14, oldNavy);
var girl = new Person(“Molly Ringwald”, 15, gap);
var caroline = new Person(“Caroline”, 17, jordache);

//now complete with uses of these objects and methods
console.log(“Getting started”);

console.log( ” has an awesome “+girl.brand.make +” jacket.”);



Tags: , , , , , , , , ,

function: AcceptGuess

The next function I had to work out was how to accept the player’s guess in a way that would set up an array in a way that each digit of the player’s guess is an entry in the array. This would give me an array containing the secretCode generated by the computer and an array containing the guess generated by the user. All I’d have to do then, in cycle through the arrays to make comparisons (If only it was so easy).

So, this function proved to be quite simple:



//accepts guess from user and parses it into an array of ‘code’ length

acceptGuess = function(code,numbers){

    console.log(“***acceptGuess function reached***”);

    var guess = prompt(“Make a guess at the secret code (“+code+”digits/1-“+numbers+”)”);

    //need to add something to ensure guess is within parameters

        console.log(“guesslength is “+guess.length);

        console.log(“code is “+code);

        var codeNum = code * 1;

        console.log(typeof guess.length);

        console.log(typeof codeNum);

        if (guess.length === codeNum){

        console.log(“got inside the loop”);

            for (i=0;i>code; i++){

                console.log(“guess[i] = “+guess[i]);

                guessDigit[i] =guess.substring(i,i+1);



            return guessDigit;

        } else {

             console.log(“invalid guess”);



The only problem I had with this was that the parameter ‘code’ comes through as a string. Note the two lines of code:

console.log(typeof guess.length);

console.log(typeof codeNum);

I’ve realized that the best way to debug is to have console.log lines everywhere that write out what is going on step by step. ‘typeof’ has been a very valuable code that I use whenever it looks like math isn’t working – so far it has uncovered the problem nearly 100% of the time.

Javascript will convert it to a ‘number’ if you do any mathematical function with the variable. An easy way to do this without messing anything up is to multiply by 1. To be even more careful, I created a new variable, ‘codeNum’, to hold this value as a number. The following line did that:

var codeNum = code * 1;


Last piece in my next post: comparing the values of the guess with the secret code. I just finished this 5 minutes ago after running myself ragged for several days (it was the reason I was dilly dallying about this posting)


Leave a comment

Posted by on July 3, 2012 in Uncategorized


Tags: , , , , , , ,