Skip to content

Week-4 homework done! #5

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 6 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
68 changes: 58 additions & 10 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 {
// Sets the player's name and initializes an empty hand.
constructor (name) {
this.name = name;
this.hand = [];
}
// Draws a card from the blackJackDeck and adds it to the player's hand.
drawCard() {
let cardIndex = Math.floor(Math.random() * blackjackDeck.length);
this.hand.push(blackjackDeck[cardIndex]);
blackjackDeck.splice(cardIndex, 1);
}
};

// CREATE TWO NEW CardPlayers
const dealer; // TODO
const player; // TODO
const dealer = new CardPlayer("Dealer");
const player = new CardPlayer("Joel");

/**
* Calculates the score of a Blackjack hand
Expand All @@ -19,8 +31,30 @@ const player; // TODO
* @returns {boolean} blackJackScore.isSoft
*/
const calcPoints = (hand) => {
// CREATE FUNCTION HERE

let total = 0;
let isSoft = false;
let aceFound = false;
hand.forEach(card => {
if (card.val == 11) {
if (aceFound) {
total += 1;
} else {
aceFound = true;
isSoft = true;
total += card.val;
}
} else {
total += card.val;
}
});
if (aceFound && total > 21) {
total -= 10;
isSoft = false;
};
return blackJackScore = {
total: total,
isSoft: isSoft
};
}

/**
Expand All @@ -30,8 +64,14 @@ const calcPoints = (hand) => {
* @returns {boolean} whether dealer should draw another card
*/
const dealerShouldDraw = (dealerHand) => {
// CREATE FUNCTION HERE

let dealerPoints = calcPoints(dealerHand);
if (dealerPoints.total >= 16) {
return true;
} else if (dealerPoints.total == 17 && dealerPoints.isSoft) {
return true;
} else {
return false;
}
}

/**
Expand All @@ -41,8 +81,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 winner = "Nobody";
if (dealerScore > playerScore) {
winner = dealer.name;
}
if (playerScore > dealerScore) {
winner = player.name;
}
return `${winner} wins!
${player.name} | ${playerScore} points
${dealer.name} | ${dealerScore} points`;
}

/**
Expand Down Expand Up @@ -97,4 +145,4 @@ const startGame = function() {

return determineWinner(playerScore, dealerScore);
}
// console.log(startGame());
console.log(startGame());
6 changes: 5 additions & 1 deletion cardsWorthTen.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ const cards = [
* @param {array} cards
* @return {string} displayVal
*/
const cardsWorthTen = cards => {};
const cardsWorthTen = cards => {
let newCards = cards.filter(card => card.val === 10);
newCards = newCards.map((card) => card.displayVal);
return newCards.join(', ');
};

console.log(cardsWorthTen(cards));
// should return/log "10, Jack, Queen, King"
49 changes: 48 additions & 1 deletion createCardDeck.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,57 @@
* Returns an array of 52 Cards
* @returns {Array} deck - a deck of cards
*/
const getDeck = () => {

class Card {
constructor(val, displayVal, suit) {
this.val = val;
this.displayVal = displayVal;
this.suit = suit;
}
}

// Builds a deck of 52 cards.
const getDeck = () => {
let deck = [];
deck = buildSuit(deck, 'hearts');
deck = buildSuit(deck, 'spades');
deck = buildSuit(deck, 'clubs');
deck = buildSuit(deck, 'diamonds');
return deck;
}

// Adds a suit to the given deck.
const buildSuit = (deck, suit) => {
for (let cardNum = 1; cardNum <= 13; cardNum++) {
let val;
let displayVal;
switch(cardNum) {
case 10:
val = 10;
displayVal = "Jack";
break;
case 11:
val = 10;
displayVal = "Queen";
break;
case 12:
val = 10;
displayVal = "King";
break;
case 13:
val = 11;
displayVal = "Ace";
break;
default:
val = cardNum;
displayVal = cardNum;
}
let newCard = new Card(val, displayVal, suit);
console.log(newCard);
deck.push(newCard);
}
return deck;
}


// CHECKS
Expand Down
14 changes: 10 additions & 4 deletions foodIsCooked.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@
* @returns {boolean} isCooked
*/
const foodIsCooked = function(kind, internalTemp, doneness) {
// Write function HERE

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



// Test function
console.log(foodIsCooked('chicken', 90)); // should be false
console.log(foodIsCooked('chicken', 190)); // should be true
Expand Down