Skip to content

Complete exercises #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 95 additions & 16 deletions blackjack.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@ const blackjackDeck = getDeck();
* @constructor
* @param {string} name - The name of the player
*/
class CardPlayer {}; //TODO
class CardPlayer {
constructor(name) {
this.name = name;
this.hand = [];
}

drawCard() {
const card = blackjackDeck[Math.floor(Math.random() * 52)];
this.hand.push(card);
}
};

// CREATE TWO NEW CardPlayers
const dealer; // TODO
const player; // TODO
let dealer = new CardPlayer('Dealer');
//dealer.drawCard();
let player = new CardPlayer('Player');
//player.drawCard();

/**
* Calculates the score of a Blackjack hand
Expand All @@ -18,20 +30,67 @@ const player; // TODO
* @returns {number} blackJackScore.total
* @returns {boolean} blackJackScore.isSoft
*/
const calcPoints = (hand) => {
// CREATE FUNCTION HERE
const calcPoints = (hand) => { //hand = [{val: 10, displayVal: "10", suit: "hearts"}, ... ]
let blackJackScore = {total: 0, isSoft: false};
let counter = 0;
let lastAce = 0;

hand.forEach(card => {
if (card.val < 11 && blackJackScore.isSoft === false) {
blackJackScore.total += card.val;
} else if (card.val < 11 && blackJackScore.isSoft === true) {
if ((blackJackScore.total + card.val) <= 21) {
blackJackScore.total += card.val;
} else if ((blackJackScore.total + card.val) > 21) {
blackJackScore.total += card.val - 10;
blackJackScore.isSoft = false;
//player.hand[lastAce].val = 1;
}
} else if (card.val === 11 && blackJackScore.isSoft === false) {
if ((blackJackScore.total + 11) <= 21) {
blackJackScore.total += 11;
blackJackScore.isSoft = true;
//lastAce = counter;
} else if ((blackJackScore.total + 11) > 21) {
blackJackScore.total += 1;
//player.hand[counter].val = 1;
}
} else if (card.val === 11 && blackJackScore.isSoft === true) {
if ((blackJackScore.total + 1) <= 21) {
blackJackScore.total += 1;
//player.hand[counter].val = 1;
} else if ((blackJackScore.total + 1) > 21) {
blackJackScore.total -= 9;
blackJackScore.isSoft = false;
//player.hand[counter].val = 1;
//player.hand[lastAce].val = 1;
}
}
counter++;
})
return blackJackScore;
}

// player.drawCard();
// console.log(player.hand[0].val);
// player.drawCard();
// console.log(player.hand[1].val);
// player.drawCard();
// console.log(player.hand[2].val);
// player.drawCard();
// console.log(player.hand[3].val);
// console.log(calcPoints(player.hand));
// console.log(calcPoints([{val: 0},{val: 0},{val: 1},{val: 11}]));

/**
* Determines whether the dealer should draw another card.
*
* @param {Array} dealerHand Array of card objects with val, displayVal, suit properties
* @returns {boolean} whether dealer should draw another card
*/
const dealerShouldDraw = (dealerHand) => {
// CREATE FUNCTION HERE

const dealerShouldDraw = (dealerHand) => { // {total: 0, isSoft: false};
const hand = calcPoints(dealerHand);
return (hand.total < 17) ? true: false;
}

/**
Expand All @@ -41,8 +100,16 @@ const dealerShouldDraw = (dealerHand) => {
* @returns {string} Shows the player's score, the dealer's score, and who wins
*/
const determineWinner = (playerScore, dealerScore) => {
// CREATE FUNCTION HERE

let winStr =
`Player Score: ${playerScore}<br>Dealer Score: ${dealerScore}<br>`;
if (playerScore > dealerScore) {
winStr += `Player Wins!`
} else if (playerScore < dealerScore) {
winStr += `Dealer Wins!`
} else if (playerScore === dealerScore) {
winStr += `It's a tie!`
}
return winStr;
}

/**
Expand All @@ -60,13 +127,16 @@ const getMessage = (count, dealerCard) => {
*/
const showHand = (player) => {
const displayHand = player.hand.map((card) => card.displayVal);
console.log(`${player.name}'s hand is ${displayHand.join(', ')} (${calcPoints(player.hand).total})`);
document.write(`${player.name}'s hand is ${displayHand.join(', ')} (${calcPoints(player.hand).total}) <br>`);
console.log(`${player.name}'s hand is ${displayHand.join(', ')} (${calcPoints(player.hand).total}) \n`);
}

/**
* Runs Blackjack Game
*/
const startGame = function() {
let playerCounter = 2;
let dealerCounter = 2;
player.drawCard();
dealer.drawCard();
player.drawCard();
Expand All @@ -78,23 +148,32 @@ const startGame = function() {
player.drawCard();
playerScore = calcPoints(player.hand).total;
showHand(player);
playerCounter++;
}
if (playerScore > 21) {
return 'You went over 21 - you lose!';
return '<br>You went over 21 - you lose!<br>';
} else if (playerScore === 21 && playerCounter === 2) {
return '<br>You have exactly 21 - you win!<br>';
}
console.log(`Player stands at ${playerScore}`);
document.write(`Player stands at ${playerScore}<br><br>`);
console.log(`Player stands at ${playerScore}\n`);

let dealerScore = calcPoints(dealer.hand).total;
while (dealerScore < 21 && dealerShouldDraw(dealer.hand)) {
dealer.drawCard();
dealerScore = calcPoints(dealer.hand).total;
showHand(dealer);
dealerCounter++;
}
if (dealerScore > 21) {
return 'Dealer went over 21 - you win!';
return '<br>Dealer went over 21 - you win!<br>';
} else if (dealerScore === 21 && dealerCounter === 2) {
return '<br>Dealer has exactly 21 - Dealer wins!<br>';
}
console.log(`Dealer stands at ${dealerScore}`);
document.write(`Dealer stands at ${dealerScore}<br><br>`);
console.log(`Dealer stands at ${dealerScore}\n`);

return determineWinner(playerScore, dealerScore);
}
// console.log(startGame());
const consoleLog = document.write(startGame());
console.log(consoleLog); //unfortunately doesn't work
16 changes: 15 additions & 1 deletion cardsWorthTen.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,21 @@ const cards = [
* @param {array} cards
* @return {string} displayVal
*/
const cardsWorthTen = cards => {};
const cardsWorthTen = cards => {
// let fullString = '';
// let firstString = 0;
// for (let i = 0; i < cards.length; i++) {
// if (cards[i].val === 10 && firstString === 0) {
// fullString += cards[i].displayVal;
// firstString++;
// } else if (cards[i].val === 10 && firstString !== 0) {
// fullString += `, ${cards[i].displayVal}`;
// }
// }

const fullString = cards.filter(card => card.val === 10).map(card => card.displayVal).join(`, `);
return fullString;
};

console.log(cardsWorthTen(cards));
// should return/log "10, Jack, Queen, King"
47 changes: 35 additions & 12 deletions createCardDeck.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,47 @@
* @returns {Array} deck - a deck of cards
*/
const getDeck = () => {

const suits = ['hearts', 'diamonds', 'spades', 'clubs'];
const cards = [];

for (let i = 0; i < suits.length; i++) {
for (let j = 1; j <= 13; j++) {
let displayVal = j;
let val = j;
if (j === 10) {
displayVal = "Jack";
val = 10;
} else if (j === 11) {
displayVal = "Queen";
val = 10;
} else if (j === 12) {
displayVal = "King";
val = 10;
} else if (j === 13) {
displayVal = "Ace";
val = 11;
}
cards.push({val, displayVal, suit: suits[i]});
}
}
return cards;
}



// CHECKS
const deck = getDeck();
console.log(`Deck length equals 52? ${deck.length === 52}`);
// const deck = getDeck();
// console.log(`Deck length equals 52? ${deck.length === 52}`);

const randomCard = deck[Math.floor(Math.random() * 52)];
// const randomCard = deck[Math.floor(Math.random() * 52)];

const cardHasVal = randomCard && randomCard.val && typeof randomCard.val === 'number';
console.log(`Random card has val? ${cardHasVal}`);
// const cardHasVal = randomCard && randomCard.val && typeof randomCard.val === 'number';
// console.log(`Random card has val? ${cardHasVal}`);

const cardHasSuit = randomCard && randomCard.suit && typeof randomCard.suit === 'string';
console.log(`Random card has suit? ${cardHasSuit}`);
// const cardHasSuit = randomCard && randomCard.suit && typeof randomCard.suit === 'string';
// console.log(`Random card has suit? ${cardHasSuit}`);

const cardHasDisplayVal = randomCard &&
randomCard.displayVal &&
typeof randomCard.displayVal === 'string';
console.log(`Random card has display value? ${cardHasDisplayVal}`);
// const cardHasDisplayVal = randomCard &&
// randomCard.displayVal &&
// typeof randomCard.displayVal === 'string';
// console.log(`Random card has display value? ${cardHasDisplayVal}`);
17 changes: 12 additions & 5 deletions foodIsCooked.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@
* @returns {boolean} isCooked
*/
const foodIsCooked = function(kind, internalTemp, doneness) {
// Write function HERE

if (kind === 'beef') {
if (doneness === 'rare'){
return internalTemp >= 125;
} else if (doneness === 'medium'){
return internalTemp >= 135;
} else if (doneness === 'well'){
return internalTemp >= 155;
}
} else if (kind === 'chicken') {
return internalTemp >= 165
}
}



// Test function
console.log(foodIsCooked('chicken', 90)); // should be false
console.log(foodIsCooked('chicken', 190)); // should be true
console.log(foodIsCooked('beef', 138, 'well')); // should be false
console.log(foodIsCooked('beef', 138, 'medium')); // should be true
console.log(foodIsCooked('beef', 138, 'rare')); // should be true
console.log(foodIsCooked('beef', 138, 'rare')); // should be true
11 changes: 10 additions & 1 deletion logCardDeck.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
(function () {
const cards = [
{ val: 2, displayVal: '2', suit: 'hearts' },
{ val: 3, displayVal: '3', suit: 'hearts' },
Expand Down Expand Up @@ -58,4 +59,12 @@ const cards = [
*
* @param {array} deck A deck of cards
*/
const logCardDeck = deck => {};
const logCardDeck = deck => {
for (let i = 0; i < deck.length; i++) {
console.log(deck[i]);
}
};

logCardDeck(cards);

})();