Skip to content

week 4 homework complete #12

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 1 commit 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
60 changes: 47 additions & 13 deletions blackjack.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@ 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 random = Math.floor(Math.random() * blackjackDeck.length);
this.hand.push(blackjackDeck[random])
blackjackDeck.splice(random, 1);
}
};

// CREATE TWO NEW CardPlayers
const dealer; // TODO
const player; // TODO
const dealer = new CardPlayer('Dealer');
const player = new CardPlayer (prompt("Enter your name here"));

/**
* Calculates the score of a Blackjack hand
Expand All @@ -18,9 +28,25 @@ const player; // TODO
* @returns {number} blackJackScore.total
* @returns {boolean} blackJackScore.isSoft
*/
const calcPoints = (hand) => {
// CREATE FUNCTION HERE

const calcPoints = (hand) => {
let aces = 0;
let isSoft = false;
let total = 0;
hand.forEach(card => {
total += card.val;
if (card.displayVal === 'ace') {
aces ++;
}
});
while (aces > 0 && total > 21) {
isSoft = true;
aces--;
total -= 10;
}
return {
total: total,
isSoft: isSoft
}
}

/**
Expand All @@ -29,9 +55,12 @@ const calcPoints = (hand) => {
* @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) => {
const score = calcPoints(dealerHand);
if (score.total < 17 || (score.total === 17 && score.isSoft === true)) {
return true;
}
return false;
}

/**
Expand All @@ -41,9 +70,14 @@ 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;
if (playerScore > dealerScore) {
winner = player;
} else {
winner = dealer;
}
return `Player scored ${playerScore} and Dealer score ${dealerScore}, the winner is ${winner.name}!`
}

/**
* Creates user prompt to ask if they'd like to draw a card
Expand Down Expand Up @@ -97,4 +131,4 @@ const startGame = function() {

return determineWinner(playerScore, dealerScore);
}
// console.log(startGame());
console.log(startGame());
9 changes: 8 additions & 1 deletion cardsWorthTen.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ const cards = [
* @param {array} cards
* @return {string} displayVal
*/
const cardsWorthTen = cards => {};

const cardsWorthTen = cards => {
const onlyTen = cards.map(card => {
if (card.val === 10) { return card.displayVal }
});
const finalCard = onlyTen.filter(card => card !== undefined);
return finalCard.join(', ');
}

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

}

// createCardDeck
// 2. Create a function getDeck that returns an array of 52 cards. There are four suits (hearts, spades, clubs, diamonds). Each suit will have 13 cards:
// 2 – 10 will have a val equal to the number
// 'Jack', 'Queen', and 'King' will all have a val of 10
// 'Ace' will have a val of 11

const getDeck = () => {
const deck = [];
const suits = ['hearts', 'diamonds', 'spades', 'clubs'];
// for each suit, we need to add 13 cards
for (let i = 0; i < suits.length; i++) {
// for each suit, we need cards 2 - 10
for (let j = 0; j < 11; j++) {
if (j < 9) {
deck.push({suit: suits[i], val: j + 2, displayVal: (j + 2).toString()})
} else if (j === 9 ) {
// for each suit, we need royalty cards
const royalty = ['jack', 'queen', 'king'];
for (let k = 0; k < royalty.length; k++) {
deck.push({suit: suits[i], val: 10, displayVal: royalty[k]})
}
} else {
// for each suit, we need an ace
deck.push({suit: suits[i], val: 11, displayVal: 'ace' })
}
}
// now the loop starts over with the next suit
}
return deck;
}

// CHECKS
const deck = getDeck();
Expand Down
15 changes: 11 additions & 4 deletions foodIsCooked.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@
* @param {string} doneness
* @returns {boolean} isCooked
*/
const foodIsCooked = function(kind, internalTemp, doneness) {
const foodIsCooked = function (kind, internalTemp, doneness) {
// Write function HERE

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



// Test function
console.log(foodIsCooked('chicken', 90)); // should be false
console.log(foodIsCooked('chicken', 190)); // should be true
Expand Down
8 changes: 7 additions & 1 deletion logCardDeck.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,10 @@ const cards = [
*
* @param {array} deck A deck of cards
*/
const logCardDeck = deck => {};
const propertyName = Object.values(cards);
for (let card of propertyName) {
const vals = Object.keys(card)
for (let val of vals) {
console.log(`${val}: ${card[val]}`)
}
}