feat: create a card counting lab (#61857)

Co-authored-by: Ilenia <26656284+ilenia-magoni@users.noreply.github.com>
Co-authored-by: Dario <105294544+Dario-DC@users.noreply.github.com>
This commit is contained in:
Anna
2025-08-26 13:55:23 -04:00
committed by GitHub
parent 2005a3d4c5
commit 156053bc24
5 changed files with 219 additions and 18 deletions
+22 -18
View File
@@ -3179,6 +3179,10 @@
"In this lab you will implement a function that converts the temperature from Celsius to Fahrenheit."
]
},
"lab-counting-cards": {
"title": "Build a Card Counting Assistant",
"intro": ["In this lab you will use JavaScript to count dealt cards."]
},
"lab-leap-year-calculator": {
"title": "Build a Leap Year Calculator ",
"intro": [
@@ -3227,6 +3231,12 @@
"In this lab, you'll review working with arrays and random numbers by building a lunch picker program."
]
},
"lab-golf-score-translator": {
"title": "Build a Golf Score Translator",
"intro": [
"For this lab, you will use array methods to translate golf scores into their nickname."
]
},
"lab-reverse-a-string": {
"title": "Build a String Inverter",
"intro": [
@@ -3269,6 +3279,12 @@
"You'll also practice using functions to randomly select a question and an answer from an array and compare them."
]
},
"lab-record-collection": {
"title": "Build a Record Collection",
"intro": [
"In this lab you will build a function to manage a record collection."
]
},
"review-javascript-objects": {
"title": "JavaScript Objects Review",
"intro": [
@@ -3421,6 +3437,12 @@
"In this lab, you will convert special characters in a string to their corresponding HTML entities."
]
},
"lab-optional-arguments-sum-function": {
"title": "Build an Optional Arguments Sum Function",
"intro": [
"In this lab you will build a function that accepts up to two arguments, and sum them, but if there is only one argument returns a function that waits for the second number to sum."
]
},
"review-javascript-fundamentals": {
"title": "JavaScript Fundamentals Review",
"intro": [
@@ -4763,24 +4785,6 @@
"exam-certified-full-stack-developer": {
"title": "Certified Full Stack Developer Exam",
"intro": ["Pass this exam to become a Certified Full Stack Developer."]
},
"lab-golf-score-translator": {
"title": "Build a Golf Score Translator",
"intro": [
"For this lab, you will use array methods to translate golf scores into their nickname."
]
},
"lab-record-collection": {
"title": "Build a Record Collection",
"intro": [
"In this lab you will build a function to manage a record collection."
]
},
"lab-optional-arguments-sum-function": {
"title": "Build an Optional Arguments Sum Function",
"intro": [
"In this lab you will build a function that accepts up to two arguments, and sum them, but if there is only one argument returns a function that waits for the second number to sum."
]
}
}
},
@@ -0,0 +1,9 @@
---
title: Introduction to the Build a Card Counting Assistant
block: lab-counting-cards
superBlock: full-stack-developer
---
## Introduction to the Build a Card Counting Assistant
For this lab, you will use JavaScript to count dealt cards.
@@ -0,0 +1,171 @@
---
id: 68a23004e96a105df3a21fc4
title: Build a Card Counting Assistant
challengeType: 26
dashedName: lab-counting-cards
---
# --description--
In the casino game Blackjack, a player can determine whether they have an advantage on the next hand over the house by keeping track of the relative number of high and low cards remaining in the deck. This is called Card Counting.
Having more high cards remaining in the deck favors the player. When the count is positive, the player should bet high. When the count is zero or negative, the player should bet low.
**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab.
**User Stories:**
1. You should use `let` to declare a global variable named `count` and set it to `0`.
1. You should have a function called `cc`.
1. The `cc` function should receive a `card` parameter which can either be a number or string.
- For values between `2` to `10`, the `card` parameter will be a number.
- For all other values, the `card` parameter will be a string.
1. The `cc` function should modify the global `count` variable based on certain criteria.
1. The global `count` variable should be increased by `1` for the cards `2`, `3`,`4`, `5`, or `6`
1. The global `count` variable should remain unchanged for the cards `7`, `8`, `9`.
1. The global `count` variable should be decreased for the cards `10`, `"J"`, `"Q"`, `"K"`, `"A"`
1. The `cc` function should return a string with current count and the string `Bet` if the count is positive.
1. The `cc` function should return a string with current count and the string `Hold` if the count is less than or equal to `0`.
1. In the function output, the current count and the player's decision (`Bet` or `Hold`) should be separated by a space. For example, `-3 Hold`.
# --hints--
You should use `let` to declare a global variable named `count` and set it to `0`.
```js
assert.match(__helpers.removeJSComments(code), /^\s*let\s+count\s*=\s*0\s*;?\s*$/m);
```
You should have a function named `cc`.
```js
assert.isFunction(cc);
```
Your function should return the value of `count` and the text (`Bet` or `Hold`) with one space character between them.
```js
count = 0;
let out = cc(10);
const pattern = /^-?\d+ (Bet|Hold)$/;
assert.match(out, pattern);
```
After the cards `2`, `3`, `4`, `5`, then calling `cc(6)` should return the string `5 Bet`.
```js
count = 0;
cc(2);
cc(3);
cc(4);
cc(5);
const out = cc(6);
assert.strictEqual(out, '5 Bet')
```
After the cards `7`, `8`, then calling `cc(9)` should return the string `0 Hold`.
```js
count = 0;
cc(7);
cc(8);
const out = cc(9);
assert.strictEqual(out, '0 Hold');
```
After the cards `10`, `"J"`, `"Q"`, `"K"`, then calling `cc("A")` should return the string `-5 Hold`.
```js
count = 0;
cc(10);
cc('J');
cc('Q');
cc('K');
const out = cc('A');
assert.strictEqual(out, '-5 Hold');
```
After the cards `3`, `7`, `"Q"`, `8`, then calling `cc("A")` should return the string `-1 Hold`.
```js
count = 0;
cc(3);
cc(7);
cc('Q');
cc(8);
const out = cc('A');
assert.strictEqual(out, '-1 Hold');
```
After the cards `2`, `"J"`, `9`, `2`, then calling `cc(7)` should return the string `1 Bet`.
```js
count = 0;
cc(2);
cc('J');
cc(9);
cc(2);
const out = cc(7);
assert.strictEqual(out, '1 Bet');
```
After the cards `2`, `2`, then calling `cc(10)` should return the string `1 Bet`.
```js
count = 0;
cc(2);
cc(2);
const out = cc(10);
assert.strictEqual(out, '1 Bet')
```
After the cards `3`, `2`, `"A"`, `10`, then calling `cc("K")` should return the string `-1 Hold`.
```js
count = 0;
cc(3);
cc(2);
cc('A');
cc(10);
const out = cc('K');
assert.strictEqual(out, '-1 Hold');
```
# --seed--
## --seed-contents--
```js
```
# --solutions--
```js
let count = 0;
function cc(card) {
switch(card) {
case 2:
case 3:
case 4:
case 5:
case 6:
count++;
break;
case 10:
case 'J':
case 'Q':
case 'K':
case 'A':
count--;
}
if(count > 0) {
return count + " Bet";
} else {
return count + " Hold";
}
}
```
@@ -0,0 +1,16 @@
{
"name": "Build a Card Counting Assistant",
"isUpcomingChange": false,
"dashedName": "lab-counting-cards",
"superBlock": "full-stack-developer",
"helpCategory": "JavaScript",
"challengeOrder": [
{
"id": "68a23004e96a105df3a21fc4",
"title": "Build a Card Counting Assistant"
}
],
"blockType": "lab",
"blockLayout": "link",
"usesMultifileEditor": true
}
@@ -318,6 +318,7 @@
"lab-email-masker",
"workshop-loan-qualification-checker",
"lab-celsius-to-fahrenheit-converter",
"lab-counting-cards",
"lab-leap-year-calculator",
"lab-truncate-string",
"lab-string-ending-checker",