JavaScript switch statement - javascript

I'm trying to write a game of Black Jack but I need some help trying to figure out how to switch the value of the Ace card between 1 & 11 as needed. This is the function that I have so far.
function cValue(card){
if (typeof(card) === "string"){
switch(card){
case 'J':
case 'Q':
case 'K':
return 10;
break;
case 'A':
return 11;
break;
}
}
else return card;
}

Quite simply: it's at another point in the logic that whether the Ace needs to be 1 or 11 needs to be determined...
But adding the ability to choose in that particular function whether it returns 1 or 11 is simple enough:
function cValue(card, aceIs1)
{
if (typeof card === "string") {
switch(card) {
case 'J':
case 'Q':
case 'K':
return 10;
case 'A':
return aceIs1 ? 1 : 11;
}
}
else return card;
}
That way you have an optional argument you can send to make the ace return 1.
cValue('A'); // -> 11
cValue('A', false); // -> 11
cValue('A', true); // -> 1
However, whether or not that is useful to your program depends on how you're writing your game.
You most likely need to do a lot more planning/structuring based around the fact that after counting the points you need to determine if there's aces in the hand and subtract 10 for each ace until you're under 21, and only if there's not enough aces to get under 21, THEN call it a bust.
Something like, if you have an array of card values like var hand = [9, 'A', 'A'] you could have a function that calculates the total hand like follows:
function calculateHand(hand)
{
var total = 0, aces = 0, card;
for (var i = 0, ii = hand.length; i < ii; i++)
{
card = hand[i];
if (typeof card === 'number') { total += card; }
else if (card === 'A') { total += 11; aces++; }
else { total += 10; }
}
while (total > 21 && aces > 0)
{ total -= 10; aces--; }
return total;
}
calculateHand(['A', 8]); // -> 19
calculateHand(['A', 8, 'A']); // -> 20
calculateHand(['A', 8, 'A', 'A']); // -> 21

Related

I'm having trouble completing my 21blackjack logic

I am required to code the logic and rules of Blackjack 21 (not a console game of Blackjack). I don't need to get the input from the keyboard, but the solution should be able to run for any scenario (players and hands). The code has to be as if it was production code, I will be reviewed on code quality. As per the test case given, determine the players' results against the dealer in a round of Blackjack, by outputting each players hand and result.
#RULES:
Cards in a hand are summed to determine the hand's value.
An ace can be equal to 1 or 11, based on the other cards in the hand. The goal is to not exceed 21.
Any player whose hand consists of 5 cards without the hand exceeding 21 automatically beats the dealer.
Players play against the dealer and not each other.
Any player whose hand is equal to or exceeds the dealer's hand and is less than or equal to 21 beats the dealer.
I need help with getting all the players' score and outputting them.
github link to my code
https://github.com/Nduh-zn/blackjack-Logic
const reducer = (a,b) => a + b;
const CalculateHand = hand=> {
const getTotal= [];
let AceQty = 0;
hand.forEach((faceCard) => {
let num = parseInt(faceCard);
//if the card value is either 'Q', 'K' or 'J', the card number should equal to 10
switch(true){
case faceCard == 'Q' || faceCard == 'K' || faceCard == 'J' : num = 10
getTotal.push(num)
break;
//case 2: determine the value for Face Card A
case faceCard = 'A': AceQty++;
let aceCheck = getTotal.reduce(reducer, 0);
if(aceCheck < 12 && AceQty>=1)
{
num = 11;
getTotal.push(num)
}
else
{
num = 1;
getTotal.push(num);
}
break;
case faceCard <= 10 : getTotal.push(num)
break;
default:
}
})
return getTotal;
}
//comparing dealer hand to player hand to determine the winner between each player vs the dealer
const calculateWinner = (theDealer, thePlayer) => {
const dealerHand = theDealer.reduce(reducer, 0);
thePlayer.forEach((item, idx) => {
let playerHand = item.reduce(reducer);
if(item.length >= 5 && playerHand <= 21)
{
console.log(`playerName + has won with 5 cards`)
}
else if(playerHand > dealerHand && playerHand <= 21)
{
console.log(`playerName + beats the dealer`)
}
else if(playerHand === dealerHand)
{
console.log(`playerName + tied with dealer`)
}
else
{
console.log(`dealer beats...`)
}
})
}
const getHand = hand => CalculateHand(hand);
(gameStart = () => {
const dealer = getHand(['6', '9']);
const Andrew = getHand(['9','6','J']);
const Billy = getHand(['Q', 'K']);
const Carla = getHand(['2','9','K']);
const Ndu = getHand(['A','k', '10'])
calculateWinner(dealer, [Andrew, Billy, Carla]);
})();

Elegant solution for determining which range a numeric value falls into

If I supply a number to a function, how would I go about validating it against a range of numbers like this?
1-10 = A
11-20 = B
21-30 = C
...
I know I can do if statements to evaluate this, but I'm looking for something more elegant because the problem gets a lot more complex and I don't want a nasty web of ifs.
var letter = "";
function getLetter(num) {
if (num >= 1 && num <= 10) {
letter = "A";
} else if (num >= 11 && num <= 20) {
letter = "B";
}
// this eventually gets gross
}
Expected outcome of getLetter(14) would be "B", and getLetter(49) would be "E", etc. Case/switch is also off the table for similar reasons.
Any other ideas welcome.
Just a point about your code
function getLetter(num) {
if (num >= 1 && num <= 10) {
letter = "A";
} else if (num >= 11 && num <= 20) {
letter = "B";
}
// this eventually gets gross
}
this can be simplified to
function getLetter(num) {
if (num >= 1) {
if(num <= 10) {
letter = "A";
} else if (num <= 20) {
letter = "B";
}
// this eventually gets gross too
}
}
But:
If it's as simple as every letter represents a range of 10 values:
function getLetter(num) {
return String.fromCharCode(65 + Math.floor((num - 1) / 10));
}
console.log(getLetter(1));
console.log(getLetter(14));
console.log(getLetter(49));
or as suggested
function getLetter(num) {
const ret = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
return ret[Math.floor((num - 1) / 10)] || "+"; // greater than 260
}
console.log(getLetter(1));
console.log(getLetter(14));
console.log(getLetter(49));
console.log(getLetter(261));
function getLetter(number) {
let ranges = {
a: 10,
b: 20,
c: 30,
underflow: 0,
overflow: Infinity
}
return Object.entries(ranges)
.sort(([ka, va], [kb, vb]) => va - vb) // because object key sort order isn't guaranteed
// though it will be in the order as declared, but
// sorting makes sense
.find(([key, value]) => number <= value)[0];
}
console.log(getLetter(5))
console.log(getLetter(17))
console.log(getLetter(20))
console.log(getLetter(30))
console.log(getLetter(31))
console.log(getLetter(0))
If the ranges are contiguous, you only need one of the boundaries
Works alright if you want to put your ranges into an object, and then loop through that
function getLetter (number) {
let ranges = {
a: [1, 10],
b: [11, 20],
c: [21, 30],
d: [31, 36],
e: [37, 40]
}
return Object.keys(ranges).find((key) => {
let currRange = ranges[key];
if (number >= currRange[0] && number <= currRange[1]) {
return key;
}
});
}
console.log(getLetter(5))
console.log(getLetter(17))
console.log(getLetter(20))
console.log(getLetter(30))
console.log(getLetter(35))
console.log(getLetter(39))

Printing 100 to 200, with three exceptions?

I am trying to write a program that prints the numbers from 100 to 200, with three exceptions:
If the number is a multiple of 3, the string "yes" should be returned instead of the number.
If the number is a multiple of 4, the string "yes and yes" instead of the number should be returned.
If the number is a multiple of both 3 and 4, the string "yes, yes and yes" instead of the number.
I am new to JavaScript so I try to do this step by step.
I wrote this code to print the numbers from 100 to 200:
function hundredTwoHundred() {
result = [];
for (let i = 100; i <= 200; i++) {
result.push(i);
}
return result;
}
console.log(hundredTwoHundred());
Then I tried to use else/if for the exceptions:
function hundredTwoHundred() {
result = [];
for (let i = 100; i <= 200; i++) {
if (i % 3 == 0) {
console.log("yes");
} else if (i % 4 == 0) {
console.log("yes and yes")
} else if (i % 3 == 0 && i % 4 == 0) {
console.log("yes, yes and yes");
} else {
result.push(i)
}
}
return result;
}
console.log(hundredTwoHundred());
The code of course, does not work. I have tried moving result.push(i) around, but I don't want to just mindlessly move things around, without knowing the reasoning behind it.
How do I use conditional operators to find these exceptions? What am I doing wrong?
Thank you.
You need to test if the number is (divisible by 3 and divisible by 4) before checking whether it's (individually) divisible by 3 or 4, otherwise the first condition if (i % 3 == 0) will evaluate to true and you'll get yes rather than yes, yes and yes. You should also push to the result in the conditionals rather than console.logging in the conditionals, since you want to create an array of numbers and yeses and then console.log the whole constructed array afterwards.
Also make sure to declare the result with const (or var, for ES5) - it's not good to implicitly create global variables.
Also, although it doesn't matter in this case, when comparing, it's good to rely on === by default rather than == - best to only use == when you deliberately want to rely on implicit type coercion, which can result in confusing behavior.
function hundredTwoHundred() {
const result = [];
for (let i = 100; i <= 200; i++) {
if (i % 3 === 0 && i % 4 === 0) {
result.push("yes, yes and yes");
} else if (i % 3 === 0) {
result.push("yes");
} else if (i % 4 === 0) {
result.push("yes and yes")
} else {
result.push(i)
}
}
return result;
}
console.log(hundredTwoHundred());
If a number is a multiple of 3 and 4, then it is a multiple of 12. I’d also use a switch statement, so you can rewrite as follow:
for (let i = 100; i <= 200; i = i + 1) {
switch (0) {
case i % 12: console.log('yes, yes and yes'); break;
case i % 4: console.log('yes and yes'); break;
case i % 3: console.log('yes'); break;
default: console.log(i);
}
}
If you want it as an array:
// Fill an array with numbers from 100 to 200
const arr = Array(101).fill().map((_, i) => i + 100);
// Map it to numbers and strings
const hundredTwoHundred = arr.map(i => {
switch (0) {
case i % 12: return 'yes, yes and yes';
case i % 4: return 'yes and yes';
case i % 3: return 'yes';
default: return i
}
});
// Print it:
console.log(hundredTwoHundred);
When you have a complex set of conditions you need to be careful with the order in which you evaluate them.
function logExceptions(start, end) {
var divisibleByThree, divisibleByFour;
for (var i = start; i <= end; ++i) {
divisibleByThree = i % 3 == 0;
divisibleByFour = i % 4 == 0;
if (divisibleByThree && divisibleByFour) {
console.log("yes, yes and yes");
}
else if (divisibleByThree) {
console.log("yes");
}
else if (divisibleByFour) {
console.log("yes and yes");
}
else {
console.log(i);
}
}
}
logExceptions(100, 200);
If you want to save the result in an array and only later print it:
function logExceptions(start, end) {
var result = [];
var divisibleByThree, divisibleByFour;
for (var i = start; i <= end; ++i) {
divisibleByThree = i % 3 == 0;
divisibleByFour = i % 4 == 0;
if (divisibleByThree && divisibleByFour) {
result.push("yes, yes and yes");
}
else if (divisibleByThree) {
result.push("yes");
}
else if (divisibleByFour) {
result.push("yes and yes");
}
else {
result.push(i);
}
}
return result;
}
console.log(logExceptions(100, 200).toString());

how do I loop back to the previous functions when stop is only prompted at the

I am facing a problem to create a loop which will loop back to the previous functions when the user does not want the program to stop (if the user wants it to stop, the program will continue with other functions).
I need to create a list of functions to do base conversion while showing the logic:
Step1: prompt for a number
Step2: prompt for an alphabet (b for Binary/o for Octal/h for Hexadecimal) as the base
Step3: convert it to a string (e.g. "108sup10 = 1101100sup2" & "63300268sup10 = 3C5E2A7sup16")
Step4: alert the string answer in a statement (e.g: Base 10 number 63300268 is 3C5E2A7 in Hexadecimal)
Step5: prompt to stop. If user's input is not "s", it will repeat step 1~4, else it continue to step 6.
Step 6: alert the max and min number entered from (repeated) step1's input.
for step 1,2,3,4,6, it is mandatory to use functions.
May I know how do I code for STEP5 in order to loop back from step 1-4 when stopping is prompted? Do I need a function for this?
//prompt to get number
function getNumber() {
var myNumber;
do {
myNumber = Number(prompt("Enter an unsigned base 10 number:")); //prompt user's input to be excecuted first
} while (myNumber < 0) //loop will run again and again as long as the number is less than zero
return myNumber;
}
//prompt to get base
function getBase() {
var myBase;
do {
myBase = (prompt("Enter b for binary, o for octal and h for hexadecimal"));
} while (!(myBase == "b" || myBase == "B" || myBase == "s" || myBase == "S"|| myBase == "h" || myBase == "H")) //loop if the input is not b, s or h
return myBase;
}
//converting the base to the number
function baseConversion(number, newBase) {
var arr = [];
if (newBase == "b" || newBase == "B") {
newBase = 2;
} else if (newBase == "o" || newBase == "O") {
newBase = 8;
}else if (newBase == "h" || newBase == "H") {
newBase = 16;
}
do { //putting the each remainder at the front of the array
arr.unshift(number%newBase);
number = Math.floor(number/newBase); //round down the divided answer
} while (number>newBase-1) //loop as long as this condition holds
arr.unshift(number);
return arr;
}
//function to string the arrays
function convertToString(number, base) {
var resultString = ""
for (var i = 0; i < results.length; i++) {
var digit = results[i];
if (digit > 9) {
switch (digit) {
case 10:
digit = 'A'
break;
case 11:
digit = 'B'
break;
case 12:
digit = 'C'
break;
case 13:
digit = 'D'
break;
case 14:
digit = 'E'
break;
case 15:
digit = 'F'
break;
}
}
resultString += digit;
}
return resultString
}
//function to alert the answer statement
function alertAnswer() {
var statement = alert("Base 10 number:" + myNumber + "is" + base + "in" + myBase);
return statement;
}
//function to find the maximum number in the array
function myMax(myArray) {
var max = myArray[0];
for (var z = 0; z < myArray.length; z++) {
if (myArray[z] > max) {
max = myArray[z];
}
}
return max;
}
//function to find the minimum number in the array
function myMin(myArray) {
var min = myArray[0];
for (var z = 0; z < myArray.length; z++) {
if (myArray[z] > min) {
min = myArray[z];
}
}
return min;
}
Sorry if I'm mistaken, but is this what you're looking for?
var promptVar = false;
do
{
//function calls for steps 1~4
prompt();
}
while (promptVar == false)
function prompt()
{
if (confirm('Do you want to continue to step 6?'))
{
promptVar = true;
} else {
promptVar = false;
}
}

Recursive function to check 2d Array

I am creating a minesweeper game for a javascript project and have run into a problem that I can't get my head round. When you click on an empty cell (one which does not have any mines around it and so does not display a number) in the minesweeper game for those who don't know, this will reveal the whole block of empty cells that are neighbouring eachother, stopping when the "wall of numbers" containing these empty blocks is found. Example below:
[1]http://datagenetics.com/blog/june12012/hole.png
This requires a recursive function to figure out which blocks are to be revealed. My code at the moment only reveals the block that is clicked on:
function revealGridContents()
{
switch (positionClickContents)
{
case 0:
ctx.drawImage(clickedImage, (xClick*20), (yClick*20));
break;
case 1:
ctx.drawImage(num1Image, (xClick*20), (yClick*20));
break;
case 2:
ctx.drawImage(num2Image, (xClick*20), (yClick*20));
break;
case 3:
ctx.drawImage(num3Image, (xClick*20), (yClick*20));
break;
case 4:
ctx.drawImage(num4Image, (xClick*20), (yClick*20));
break;
case 5:
ctx.drawImage(num5Image, (xClick*20), (yClick*20));
break;
case 6:
ctx.drawImage(num6Image, (xClick*20), (yClick*20));
break;
case 7:
ctx.drawImage(num7Image, (xClick*20), (yClick*20));
break;
case 8:
ctx.drawImage(num8Image, (xClick*20), (yClick*20));
break;
};
};
The number passed into the switch statement is the value of the data in the array grid[xClick][yClick]; eg a 4 symbolises a block with 4 mines around it, so the image for 4 will be displayed.
Case 0 is the case that a blank block is clicked and so the code for this needs modifying but I really can't think what to do.
From what I can understand, I will need to call the revealGridContents(); function from case 0, but passing in new values for xClick and yClick (the x and y values of the array position) for each square that I want to check.
Any help shedding the light on what I need to do next would be greatly appreciated!
Without knowing slightly more of your program it's hard to give you an exact solution. You'll probably need a separate function to do this, as just using the same function will reveal everything (which is obviously not how the game works). You'll also need some way of keeping track of revealed cells, otherwise you'll get into a loop (I'm assuming this is stored in another 2d array revealed[x][y]).
You probably want to do something like this (I haven't tested this so there may be errors in it - apologies):
function revealGridContents(){
switch (positionClickContents){
case 0:
ctx.drawImage(clickedImage, (xClick*20), (yClick*20));
checkAdjacentCells(xClick, yClick);
break;
...
}
}
function checkAdjacentCells(x,y){
var cellsToCheck = [
[x,y+1],
[x,y-1],
[x+1,y],
[x-1,y]];
var x,y;
for(var i=0; i<=cellsToCheck.length; i++){
x = cellsToCheck[i][0];
y = cellsToCheck[i][1];
if(!revealed[x][y] && grid[x][y] == 0){
ctx.drawImage(clickedImage, x*20, y*20);
checkAdjacentCells(x,y);
}
}
}
Just as a general advice, you need a better separation between the model of your game and the UI.
Here is the begining of my interpretation of the minesweeper game:
function init() {
var i,j; // indexes
map = []; // global map, because i'm lazy
for (i=0; i<10; i++) {
var row = [];
for (j=0; j<10; j++)
row.push({
bomb : Math.round(Math.random()-0.4), // set bombs randomly, change to your correct ratio
revealed : false, // nothing is visible at start
count : 0 // counts will be computed after all the map is created
});
map.push(row);
}
// set adjacent bomb counts
for (i=0; i<10; i++)
for (j=0; j<10; j++) {
if (map[i-1] && map[i-1][j-1] && map[i-1][j-1].bomb) map[i][j].count++;
if (map[i-1] && map[i-1][j] && map[i-1][j].bomb) map[i][j].count++;
if (map[i-1] && map[i-1][j+1] && map[i-1][j+1].bomb) map[i][j].count++;
if (map[i] && map[i][j-1] && map[i][j-1].bomb) map[i][j].count++;
if (map[i] && map[i][j+1] && map[i][j+1].bomb) map[i][j].count++;
if (map[i+1] && map[i+1][j-1] && map[i+1][j-1].bomb) map[i][j].count++;
if (map[i+1] && map[i+1][j] && map[i+1][j].bomb) map[i][j].count++;
if (map[i+1] && map[i+1][j+1] && map[i+1][j+1].bomb) map[i][j].count++;
}
}
function print() { // uses console to display instead of canvas
var output = '\n';
for (var i=0; i<10; i++) {
for (var j=0; j<10; j++) {
var item = map[i][j];
output += (item.revealed ? item.count : 'x') + ' ';
}
output += '\n';
}
console.log(output);
}
function click(x,y) {
reveal(x,y);
print(map);
}
function reveal(x,y) {
// break early if click is invalid (invalid clicks are generated)
if (x < 0 || x > 9 || y < 0 || y > 9 || map[x][y].revealed) return;
// mark the square as clicked
map[x][y].revealed = true;
if (map[x][y].bomb) { // losing click
console.log('You lost');
} else if (map[x][y].count === 0) { // click on 0 adjacent bombs
reveal(x-1, y);
reveal(x, y-1);
reveal(x, y+1);
reveal(x+1, y);
}
}
init();
console.log('First print');
print();
console.log('Click 1,3');
click(1,3);
The difficult part is in the click() function.
Try this demo (click 'run with JS' several times until you don't lose and hit a 0):
http://jsbin.com/iqeganU/1/edit

Categories