Skip to content

added responses to assignments #10

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
81 changes: 71 additions & 10 deletions blackjack.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
const blackjackDeck = getDeck();



/**
* Represents a card player (including dealer).
* @constructor
* @param {string} name - The name of the player
*/
class CardPlayer {}; //TODO

class CardPlayer {
constructor(name){
this.name = name;
this.hand = [];
this.drawCard = function(){
let cardNumber = Math.floor(Math.random() * 52);
this.hand.push(blackjackDeck[cardNumber]);
}
}
}; //TODO

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



/**
* Calculates the score of a Blackjack hand
Expand All @@ -18,9 +32,38 @@ const player; // TODO
* @returns {number} blackJackScore.total
* @returns {boolean} blackJackScore.isSoft
*/

const calcPoints = (hand) => {
// CREATE FUNCTION HERE
let aceAmount = 0;
let blackJackScore ={
isSoft: true ,
total: 0
}

for (let i = 0; i < hand.length; i++){
if(hand[i].displayVal === 'Ace'){
aceAmount += 1;
}
if(hand[i].displayVal === 'Ace'){
if(aceAmount > 1){
if(hand[i].displayVal === 'Ace'){
aceAmount += 1
hand[i].val = 1
}else{
continue
}
}
}
blackJackScore.total +=hand[i].val
}

if(aceAmount > 0){
blackJackScore.isSoft = true;
return blackJackScore.total;
}else{
blackJackScore.isSoft = false;
return blackJackScore.total
}
}

/**
Expand All @@ -30,7 +73,13 @@ const calcPoints = (hand) => {
* @returns {boolean} whether dealer should draw another card
*/
const dealerShouldDraw = (dealerHand) => {
// CREATE FUNCTION HERE
let dealerTotal = function dealerTotalFunc (dealerHand){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm this seems to be digging too deep to get the desired result. All you need from this is get the score object from calcPoints(dealerHand) and then see how it compares to the rules in the specification (<=16, >17, or 17 && isSoft).

The function inside the function is not entirely necessary given the other code available.

calcPoints();
return (score > 16);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't see where this score is coming from . . .

}

return dealerTotal;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since dealerTotal is a function that is declared inside the dealerShouldDraw function, what we end up with here is that dealerShouldDraw actually returns the function . . . but since it is actally a value then when it is evaluated on line 147, it always looks to be true . . . that's why the program does not break.

JavaScript is really flexible, so it will totally allow weird things like this to go through and the result is a sort of false-positive and no errors getting thrown.

This function should get a full refactor, but here's the steps you are aiming for:

It should take a 'hand' object as an argument . . . .
Then store a const that is the object returned when you call calcPoints(hand) . . . .
Then a bit of if/else logic comparing the total to the dealer rules, to result in returning a boolean.



}

Expand All @@ -41,8 +90,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 playerScore = player.calcPoints(hand);
let dealerScore = dealer.calcPoints(dealerHand);*/

if(playerScore <= 21){
return playerwin;
} else if (dealerScore <= 21){
return dealerwin;
} else if (playerScore === dealerScore){
return "It's a draw";
}
}

/**
Expand Down Expand Up @@ -72,7 +129,7 @@ const startGame = function() {
player.drawCard();
dealer.drawCard();

let playerScore = calcPoints(player.hand).total;
let playerScore = calcPoints(player.hand);
showHand(player);
while (playerScore < 21 && confirm(getMessage(playerScore, dealer.hand[0]))) {
player.drawCard();
Expand All @@ -84,17 +141,21 @@ const startGame = function() {
}
console.log(`Player stands at ${playerScore}`);

let dealerScore = calcPoints(dealer.hand).total;
let dealerScore = calcPoints(dealer.hand);
while (dealerScore < 21 && dealerShouldDraw(dealer.hand)) {
dealer.drawCard();
dealerScore = calcPoints(dealer.hand).total;
dealerScore = calcPoints(dealer.hand);
showHand(dealer);
}
if (dealerScore > 21) {
return 'Dealer went over 21 - you win!';
}


console.log(`Dealer stands at ${dealerScore}`);


return determineWinner(playerScore, dealerScore);

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



const cardsWorthTen = cards => {

const cardValsFilter = cards.filter(item => item.val===10);

const cardDisplayVals = cardValsFilter.map(a => a.displayVal);
;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ope, who's this little semicolon sneaking in here? JavaScript is good at trying to ignore weird syntax errors, but still good to keep your code clean so that debugging is easier.

Are you using VSCode? Get in the habit of using it's auto-formatting tools as much as possible. It will enforce auto-indentations and even help you find syntax mistakes and allow for overall more readable code which makes it easier to reason about and refactor. tools called 'Linters' are specifically for this kind of thing, you could try one called Prettier that integrates right into VSCode.

let cardDisplayValsString = cardDisplayVals.join (', ')

return cardDisplayValsString;

};

console.log(cardsWorthTen(cards));
// should return/log "10, Jack, Queen, King"
112 changes: 112 additions & 0 deletions createCardDeck.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,121 @@
* Returns an array of 52 Cards
* @returns {Array} deck - a deck of cards
*/

/*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 suits = ['hearts', 'spades', 'clubs', 'diamonds'];
const cards = [];

suits.forEach(cardGenerate);


function cardGenerate(suit){
let cardCount = 0

for (let i = 0; i < 13; i++){
cardCount += 1;
cards.push(cardCount);
}
}
console.log(cards);
return cards;

};*/

const getDeck = () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this certainly gets the job done in a thoroughly imperative way . . . but take a step back and look at ways you might be able to refactor this down. There's a lot of repeated code in here, which always highlights an opportunity to make a reusable function that can be called each time needed.

But even more so, think of the values that are available to you when you execute a loop. Or perhaps small data-sets you can define ahead of time that you can put to work using index references in the loops, like saying const suits = ['hearts', 'clubs', 'spades', 'diamonds'], then referring to that instead of repeating those strings in several loops.

Not to say this is not a good start . . . a lot of coding is basically figuring out your first working mental model, then stepping back to review the whole, and look for ways to refactor for increased simplicity or readability. Most code you see out in the world started out looking really different and finally comes together after multiple revisions, and that's a great exercise.

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

let cardCount = 0

for(let i = 0; i < 13; i++){
if(i === 0){
cardCount += 1;
cards.push({val : 11, suit: suits[0], displayVal : 'Ace'})
} if(i > 0 && i < 10){
cardCount += 1;
cards.push({val : cardCount, suit: suits[0], displayVal : cardCount.toString()});
} if(i === 10){
cardCount += 1;
cards.push({val : 10, suit: suits[0], displayVal : 'Jack'});
} if(i === 11){
cardCount += 1;
cards.push({val : 10, suit: suits[0], displayVal : 'Queen'});
} if(i === 12){
cardCount += 1;
cards.push({val : 10, suit: suits[0], displayVal : 'King'});
}
}
cardCount = 0;
for(let i = 0; i < 13; i++){
if(i === 0){
cardCount += 1;
cards.push({val : 11, suit: suits[1], displayVal : 'Ace'})
} if(i > 0 && i < 10){
cardCount += 1;
cards.push({val : cardCount, suit: suits[1], displayVal : cardCount.toString()});
} if(i === 10){
cardCount += 1;
cards.push({val : 10, suit: suits[1], displayVal : 'Jack'});
} if(i === 11){
cardCount += 1;
cards.push({val : 10, suit: suits[1], displayVal : 'Queen'});
} if(i === 12){
cardCount += 1;
cards.push({val : 10, suit: suits[1], displayVal : 'King'});
}
}

cardCount = 0;
for(let i = 0; i < 13; i++){
if(i === 0){
cardCount += 1;
cards.push({val : 11, suit: suits[2], displayVal : 'Ace'})
} if(i > 0 && i < 10){
cardCount += 1;
cards.push({val : cardCount, suit: suits[2], displayVal : cardCount.toString()});
} if(i === 10){
cardCount += 1;
cards.push({val : 10, suit: suits[2], displayVal : 'Jack'});
} if(i === 11){
cardCount += 1;
cards.push({val : 10, suit: suits[2], displayVal : 'Queen'});
} if(i === 12){
cardCount += 1;
cards.push({val : 10, suit: suits[2], displayVal : 'King'});
}
}

cardCount = 0;
for(let i = 0; i < 13; i++){
if(i === 0){
cardCount += 1;
cards.push({val : 11, suit: suits[3], displayVal : 'Ace'})
} if(i > 0 && i < 10){
cardCount += 1;
cards.push({val : cardCount, suit: suits[3], displayVal : cardCount.toString()});
} if(i === 10){
cardCount += 1;
cards.push({val : 10, suit: suits[3], displayVal : 'Jack'});
} if(i === 11){
cardCount += 1;
cards.push({val : 10, suit: suits[3], displayVal : 'Queen'});
} if(i === 12){
cardCount += 1;
cards.push({val : 10, suit: suits[3], displayVal : 'King'});
}
}

return cards;

};



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

}
const foodIsCooked = function(kind, internalTemp, doneness) {
if(kind === 'chicken' && internalTemp >= 165){
return true;
} else if (kind === 'beef' && internalTemp >= 125 && doneness === 'rare'){
return true;
} else if (kind === 'beef' && internalTemp >= 135 && doneness === 'medium'){
return true;
} else if (kind === 'beef' && internalTemp >= 155 && doneness === 'well'){
return true;
}else{
return false;
}
};



Expand All @@ -17,4 +26,4 @@ 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
21 changes: 20 additions & 1 deletion logCardDeck.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,23 @@ const cards = [
*
* @param {array} deck A deck of cards
*/
const logCardDeck = deck => {};



const logCardDeck = deck => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So check this one out . . .. as written this function is not a reusable piece of code. You defined a parameter for 'deck' which would allow you to call this function with any external deck argument, but within the function you are using the cards array which is just a hard coded value in this file. so it "works" when called on line 80, but it would not be reusable in any other circumstance, like if you exported the function to some other program that might have a different deck not named cards in the global scope.


let vals = cards.map(a => a.val);
let dispVals = cards.map(a => a.displayVal);
let suits = cards.map(a => a.suit);


const cardValsFilter = cards.filter(item => item.suit !== false );
let cardCount = cardValsFilter;

for (i = 0; i < cardCount.length; i++){
console.log(`this is Card # ${Object.keys(cardCount)[i]}: It has a Value of ${vals[i]}, it has a Display Value of ${dispVals[i]}, and it belongs to the ${suits[i]} suit.`)
}

};

logCardDeck();