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

Conversation

miguerurso
Copy link

No description provided.




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.

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 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.

@@ -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.

// CREATE FUNCTION HERE
let dealerTotal = function dealerTotalFunc (dealerHand){
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 (score > 16);
}

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants