In the last lesson we added our first math option where the player chooses the winner of each round, the greater number. This time we’re going to add the integer option. That means that the red cards will be negative numbers. There are a few things I had to do in order to accomplish this.

I created a variable called integerChoice and gave it the value of 0. That’s “off.” I could say, var integerChoice = false; When the user says yes to using integers, then I could change it to true and then say, “if (integerChoice) {do this}”. To me that’s a fragment, and I like sentences. So, I use my 0 and 1 binary system instead. When the user clicks on “yes” for integers, then integerChoice is set to equal 1, the on position.

Another thing that had to happen is turning my number into negative numbers for hearts and diamonds, suits 1 and 2. That could be done a number of ways. I could have done 0 – number1 to get the negative of number1. And there are many other ways as well. Here I multiplied by negative one to get the negative value.

if (integerChoice == 1){

if (suit1 == 1){

number1 = (-1)*number1;

}

if (suit1 == 2){

number1 = (-1)*number1;

}

if (suit2 == 1){

number2 = (-1)*number2;

}

if (suit2 == 2){

number2 = (-1)*number2;

}

}

The final step I didn’t realize at first until my code wasn’t working. My hearts and diamonds weren’t writing on the cards. I went and found the code where they write on the page and found, i < number1. Well, when number1 is a negative number, i is never going to be less than it. I didn’t want to change it back, so I just use the absolute value of the number. The absolute value is basically the positive value of a number; it is its distance from zero. The absolute value of 3 and -3 is 3.

The code for that is Math.abs(number1) and I just slipped that right in for number1 in the for loop where it appends the suit onto the card.

i=0; i<(Math.abs(number1)); i++ //and the same needs to happen for number2

$firstPlayerSuit.append(suit1);

Right below there in the code, the face cards use number to define themselves. Instead of using absolute value this time, I used the OR operator. The two || lines means or. The card will be a jack if number1 is 11 or if number1 is -11.

if (number1 == 11 || number1 == -11)

You can watch this lesson.

### Like this:

Like Loading...