# Category Archives: Coding

## Coding Challenge

Recently, there have been a couple new revelations about number theory published in Science Within the article was a pair of theories about prime numbers that I had never heard before, one of which was:

Goldbach’s conjecture, [which] makes two assertions: that every even number greater than 2 is the sum of two primes, and that every odd number greater than 5 is the sum of three primes.

I thought it would be fun to start with the first part of this problem and write a program to accept user input in the form of an even integer > 2  and then look for the two primes whose sum is equal to the user provided:

prime1 + prime2 = user input

where prime1 and prime2 may be any prime number (even the same number twice)

I could easily see this escaping the processing power of my machine if the numbers get high, but I think it shouldn’t be too hard to at least write a code that could look for them and demonstrate whether this worked with known input.

Are you up for a quick challenge?

Learn Fractions with Zombies

If so, submit your documented answer here as a comment. Feel free to use any language you would like (I just did it in C++, but I’m eager to see better answers than my own). My favorite submissions will win a free copy of  my iBook, In Parts, Tales of Fractional Zombies, which you can enjoy yourself or regift to a youngster in your life who wants a fun way to learn the concept of fractions.

You can use these links as resources to help check your work:

If you are new to coding and are looking for a coding environment to work in, check out this posting for help setting up a C++ coding environment using Xcode (on your mac)

Posted by on June 19, 2013 in Codecademy, Coding

## Finishing Touches

Step 6: You get a hand, Determining scores and judging winner

Here are the last touches that make this code into the bare-bones structure of the game. Cards are dealt out to two hands (myHand and yourHand), each card is given a suit and a value and the two hands are compared to determine a winner. (I haven’t added anything about ‘Hitting’ or ‘Holding’, and since you start with only two cards, you can’t go over 21. I’m assuming that these things are all part of the final project).

//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”);

switch(this.suit){

case 1:

this.suit = “clubs”;

break;

case 2:

this.suit = “diamonds”;

break;

case 3:

this.suit = “hearts”;

break;

case 4:

break;

}

};

this.getValue = function(n){

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

if (n === 1){

this.value = 11;

}

else if (n >= 11){

this.value = 10;

}

else{

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);

};

//Score function

var score = function(one, two){

return one+two;

};

//comparison of hands

var judgement = function(mine,yours){

if (mine>yours){

console.log(“I win “+mine+ ” vs “+yours);

}

else if (yours>mine){

console.log(“You win “+mine+ ” vs “+yours);

}

else{

console.log(“We tied “+mine+ ” vs “+yours);

}

};

//Main Function

var myHand = new Hand();

myHand.card1.getSuit(myHand.card1.suit);

myHand.card1.getValue(myHand.card1.number);

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

myHand.card2.getSuit(myHand.card2.suit);

myHand.card2.getValue(myHand.card2.number);

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

var yourHand = new Hand();

yourHand.card1.getSuit(yourHand.card1.suit);

yourHand.card1.getValue(yourHand.card1.number);

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

yourHand.card2.getSuit(yourHand.card2.suit);

yourHand.card2.getValue(yourHand.card2.number);

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

var myScore = score(myHand.card1.value,myHand.card2.value);

var yourScore = score(yourHand.card1.value,yourHand.card2.value);

judgement(myScore,yourScore);

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”);

switch(this.suit){

case 1:

this.suit = “clubs”;

break;

case 2:

this.suit = “diamonds”;

break;

case 3:

this.suit = “hearts”;

break;

case 4:

break;

}

};

this.getValue = function(n){

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

if (n === 1){

this.value = 11;

}

else if (n >= 11){

this.value = 10;

}

else{

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();

myHand.card1.getSuit(myHand.card1.suit);

myHand.card1.getValue(myHand.card1.number);

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

myHand.card2.getSuit(myHand.card2.suit);

myHand.card2.getValue(myHand.card2.number);

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.

## Next steps… making the card objects

Step4: Adding the Card Class (called from Deal function)

//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

function Card(s,n){

this.number = n;

this.suit = s;

getSuit (method to interpret suit numbers)

getValue(method to interpret value unbers)

}

//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();

New text is highlighted in red(Although I’m not sure this formatting will follow through to the post). Here we are adding a function call at the end of the ‘deal’ function in the form of a ‘return’ statement. This means that the deal function will generate two random numbers for us and then send these to the ‘card’ constructor to make our new card objects.

I’m leaving most of the card object unfinished at this point. We will come back later to add methods for interpreting the information associated with each card object.

## Getting Started with rebuilding blackjack from scratch with notes (help requested)

Using the flowchart in the previous post, I’ve started walking through how to build this program.

Step 1: Outline:

Hand Constructor

Card Constructor

getSuit (method to interpret suit numbers)

getValue(method to interpret value unbers)

Deal function

Main Function

Step2: Fleshing out early program (make myHand)

//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

getSuit (method to interpret suit numbers)

getValue(method to interpret value unbers)

Deal function

//Main Function

var myHand = new Hand();

The main function immediately generates a hand “myHand” for the player.

Going to the Hand constructor, we know that we need two cards, and that they will be made by calling the deal function. I’ve included a console.log to immediately provide feedback showing that the cards have correct properties (simple numbers right now)

Step3: Generating cards with the ‘Deal’ function

//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

getSuit (method to interpret suit numbers)

getValue(method to interpret value unbers)

//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);

};

//Main Function

var myHand = new Hand();

Deal is called twice from the Hand Constructor. We should flesh out the deal function now. This function generates two random numbers (‘suit’ : 1-4) and (‘number’ : 1-13).

As before, I’ve added some console.log statements to make sure this is working correctly. I include a statement about being in the deal function as a check for how the program is walking through its steps.

Problem of Scope: am I making variables with appropriate scope in this function?

Posted by on July 24, 2012 in Codecademy, Coding

## Blackjack Flow

In order to make sense of the blackjack project, I made up this flow diagram to describe how the program creates ‘Hand’ objects consisting of two ‘card’ objects, and that these cards have properties that need to be generated and interpreted. The two properties are number (1-13, where 1 is Ace, 11 is Jack, 12, is Queen, etc) and suit (1-4, where each number is a given suit).

The properties of the cards can be set using a function, deal.

Once cards are made, they use two methods (because we are handling objects) to interpret the suit and value of each card.

Finally, we have two cards in a hand and can proceed to the next step…

Here’s a flow diagram of this process:

1 Comment

Posted by on July 24, 2012 in Codecademy, Coding

## Blackjack game is driving me crazy

I’ve been working on the blackjack name for codecademy all day today without luck.  The challenge / puzzle / assignment is to have a hand of cards generated and scored.

The hand is an object that includes two ‘card’ objects. each card is generated randomly with two features (number and suit*).The suit is defined by a random number 1-4 and defined by a method for matching this numeral to a suit. The number is defined as 1-13 and set to values such that 1 (ace) = 11, 1-10 =face value and 11-13 are face cards worth 10pts each.

I simply can’t do it. My code looks reasonable to me (but I don’t really feel comfortable with it at all), but I’m getting crazy results. Here’s what I have, try copying it into a javascript field and see if you can make heads or tails of it:

// Make your card constructor again here, but make sure to use private
// variables!
function Card(s,n){
this.number = n;
this.suit = s;

this.getSuit = function(suit){
switch (this.suit){
case 1:
this.suit = “clubs”;
break;
case 2:
this.suit = “diamonds”;
break;
case 3:
this.suit = “hearts”;
break;
case 4:
break;
}
}

this.getValue = function(number){
if(this.number === 1){
//console.log(“value = 11”);
this.value = 11;
return 11;
}
else if (this.number >=11){
//console.log(“value = 10”);
this.value = 10;
return 10;
}
else{
//console.log(“value = “+ this.number);
this.value = number;
return this.number;
}
};
};

// Make a deal function here. It should return a new card with a suit
// that is a random number from 1 to 4, and a number that is a random
// number between 1 and 13
var deal = function(){
var suit = Math.floor(Math.random()*4)+1;
console.log(suit);
var number = Math.floor(Math.random()*13)+1;
console.log(number);
return new Card(suit,number);
};

var Hand = function(){
this.card1 = deal();
console.log(“card1 is “+card1.number+ ” of “+card1.suit);
this.card2 = deal();
console.log(“card2 is “+card2.number+ ” of “+card2.suit);
};

Hand();
console.log(Hand);

*by the way, it is clear that there is nothing that prevents the same card from being drawn twice from what presumably is a single deck.

Posted by on July 24, 2012 in Codecademy, Coding

## Just a quick note, then off to bed

Codecademy is now adding python modules. I look forward to pursuing this even more than the javascript courses (although I’ll keep up with them as well). I’ve also just signed up as a beta tester for new javascript and python courses. You should do the same if you’re also interested in coding and education.

Posted by on July 9, 2012 in Codecademy, Coding

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 have 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){
switch(brand){
case jordache:
this.make = “Jordache”;
this.buttons = 5;
this.color = “blue”;
break;

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

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

}

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

//console.log(girl.brand.make);
console.log(girl.name+ ” has an awesome “+girl.brand.make +” jacket.”);
girl.wash();
girl.wash();
geek.sayHello();
geek.wash();
girl.sew();
girl.sew();

console.log(“finished”);

## Full code for the mastermind game

Here’s the whole thing as I’ve written it – my first full program ever!

Mastermind code in Javascript

Took me about a week to set up properly

Finally succeeded July 2, 2012

Plays all basics of the game – doesn’t show a running list to help player, but shows all moves at finish.

//gets the number of digits used

var code = function(){

var enteredCode = 0;

var codeVeritas = false;

while (codeVeritas === false){

enteredCode = prompt(“How many digits shall the code be? (1-6)”);

//verify input

if (enteredCode > 0 && enteredCode <= 6){

console.log(“We will be using “+ enteredCode+ ” digits in our code”);

codeVeritas = true;

return enteredCode;

} else{

}

}

};

//gets numerals to be used in code

var numbers = function(){

var enteredNumber = 0;

var numbersVeritas = false;

while (numbersVeritas === false){

enteredNumber = prompt(“code will consist of numerals 1 – : (1-9)”);

if (enteredNumber>0 && enteredNumber <=9){

console.log(“We will be using numerals from 1 to “+ enteredNumber+ ” for our code”);

numbersVeritas = true;

return enteredNumber;

} else{

numbersVeritas = false;

}

}

};

//sets up an array of numbers that will be the secret code

// code is the number of digits, number is the integers used for each digit

setSecretCode = function(code,numbers){

var secretCode = “0”;

var secret = [];

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

secret[i] = Math.floor(Math.random()*numbers+1);

}

return secret; // returns result to be used going forward

};

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

acceptGuess = function(code,numbers){

var guess = 0;

var guessDigit = [];

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

var codeNum = code * 1; // to transform code into a ‘number’

if (guess.length === codeNum){

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

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

}

return guessDigit;

} else {

console.log(“invalid guess”);

}

};

var compareGuess = function(code,secrets,guesses){

var green = 1-1; //right color, right position

var yellow = 1-1; //right color, wrong position

var white = 1-1; //wrong color

code = code*1; //converts code to a number type

//part 0: set ‘Already’ arrays to false

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

};

//part 1: check for perfect matches, alter guesses and secrets

//array positions to eliminate recount – count greens

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

guesses[i] = guesses[i] *1;

//console.log(“I’m in the first loop”);

//console.log(“guess”+i+”=”+guesses[i]+” of type “+typeof guesses[i]);

//console.log(“secret”+i+”=”+secrets[i]+” of type “+typeof secrets[i]);

if (guesses[i] === secrets[i] &&

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

green++;

}

}

//part2: check for imperfect matches, alter guesses and secrets

//array positions to eliminate recount – count yellows

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

for (j=0; j<code; j++){

if (guesses[i] === secrets[j] &&

yellow++;

}

}

}

//part 3: set white to number of unmatched items

//provide feedback to player

white = code – (green+yellow);

alert(green+” green    ” + yellow+” yellow”+ white+” white”);

console.log(green+” green ” + yellow+” yellow”+ white+” white”);

if (green === code){

exitShowResultLoop = true;

}

};

//main function

var guessArray = [];

var exitShowResultLoop = false;

var enteredCode = code(); // brings the variable  enteredCode out of the function

var enteredNumber = numbers(); // brings the variable enteredNumber out of the function

var secret = setSecretCode(enteredCode,enteredNumber);

//printout of array for debugging

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

//play the game using a while loop to loop until correct guess is made

while (exitShowResultLoop === false){

guessArray = acceptGuess(enteredCode, enteredNumber);

console.log(“guess is: “+guessArray);

compareGuess(enteredCode, secret, guessArray);

}

console.log(“You’ve done it!!! Congratulations!!”);