I want to add cards from the deck until I get 17 or more if the dealer's total doesn't exceed 17.
Where the error occurs.
When the total value of two cards is not 17 or more, I want to add cards until it becomes 17 or more.
However, with this code, I tried to add cards until I got 17 or more, but I added the same card as the second card (the card that was turned up by pressing the stand button). It's closed.
How do I add cards until I reach 17 or above?
The problem is caused by your for loop, after calling dealer_3Img in function turnDealer, there is a "break", commented below:
function turnDealer(dealerTotal) {
if(dealerTotal < 17){
for (let i = dealerTotal; dealerTotal < 17; i++) {
console.log(i);
//Get a new card from the array
var card = deck.pop();
dealer_3Img(card);
break //this will exit the loop
}
}
}
please read the usage of "break": The break statement "jumps out" of a loop.
https://www.w3schools.com/js/js_break.asp
To fix the problem, you can use while as #Aaron suggested, you can use while loop to solve your problem: https://www.w3schools.com/js/js_loop_while.asp
Try changing your code to:
function turnDealer(dealerTotal) {
while (dealerTotal < 17) {
console.log(i);
//Get a new card from the array
var card = deck.pop();
//need to update dealerTotal here, e.g. dealerTotal += card
dealer_3Img(card);
}
}
Forgive me but I don't play cards much.. I'm not sure if each card has a different dealerTotal so my example is treating value 17 as 17 cards
// If the dealer does not exceed 17, add cards until it exceeds 17.
function turnDealer (dealerTotal) {
var card = deck.pop()
var cardValue=1 //if cardValue is ever not 1, set it to whatever(since idk)
if (dealerTotal <17) {return turnDealer(dealerTotal+cardValue)}
return true //it has finished selecting
//usage: turnDealer(1) would make it take 17 cards, turnDealer(0) would make it take 18, turnDealer(2) would make it take 16 and so on
}
Related
so I have 2 numbers that gets randomly generated from 1 to 13 but if a number is above 10 it gets treated as 10 similar to the blackjack game
function getRandomNumber2() {
let random = Math.floor(Math.random() * 12 + 1);
if (random > 10) {
return 10;
} else {
return random;
}
}
then i attached these numbers to variables then I put them in a list and I summed them
secondCard = getRandomNumber2()
cardsList = [firstCard, secondCard];
sumGAME = firstCard + secondCard;
now I want these numbers to show in the website as images but I can still sum them and all, let's say I got number 2 then the image attached to number 2 gets displayed and if i ask for new set of numbers then the images gets deleted and gets replaced with the new pic
what I tried to do
I tried to identify each one of the 13 images as an individual variable and put if statement that looks like this
if(cardsList[0]===1)
{cards.appendChild(card1)}
and kept going for all the cards
the issue
if I ask for new set of cards it doesn't delete the old images but adds 2 more to it
I want to generate a sudoku puzzle using nodejs and recursion.
The state of my program is as follows
I start with an array of 81 zeros.
Next, I fill the 1st, 4th, and last block with nine different numbers from 1-9 because I can generate these numbers without worrying about blocks, rows, or columns.
The next step is the part I am stuck on, which is solving the actual sudoku.
I have created a "getSudokuNumbers" function which returns all of the numbers in the block, row, and column for a given cell.
Then I check if there is a number in that cell that is a possible solution
However, when the program is run there is sometimes an issue where there is no valid solution.
Essentially, my program doesn't try every solution to the puzzle, which is what I need it to do...
You can check out my codesandbox here https://codesandbox.io/s/bold-ishizaka-cn6g3?file=/src/index.js
Below highlights the important parts of the program
sudokuArr = new Array(81).fill(0);
// fills the first, fourth, and last block with numbers from 1-9
fillGrid()
const solveGrid = () => {
// loop over every cell in the sudokuArr
for (let i = 0; i < sudokuArr.length; i++) {
// gets the index of the cell's block, row, and col
const { block, row, col } = getSudokuVars(i);
// gets the numbers in the current cell's row, column, and block
const blockNums = getNumbersInBlock(block),
rowNums = getNumbersInRow(row),
colNums = getNumbersInCol(col);
// all the numbers to try for every cell
const numsToTry = [1, 2, 3, 4, 5, 6, 7, 8, 9];
numsToTry.forEach((num) => {
// check that the number is a valid sudoku number
// however, this is where the problem arrises because sometimes the blockNums, colNums, and rowNums include every number from 1-9 and the program does not do anything.
// I believe this is where I need some sort of recursion/brute force algorithmn to
// try ever possible solution
if (
!blockNums.includes(num) &&
!rowNums.includes(num) &&
!colNums.includes(num)
)
return (sudokuArr[i] = num);
});
}
};
Can anyone find out what is wrong with this code? I run the code on CodeWars and pass every test except one... sadly it does not display what the input was for that specific test so it is very difficult to figure it out.
Here are the challenge instructions:
The new "Avengers" movie has just been released! There are a lot of people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 dollars bill. A "Avengers" ticket costs 25 dollars.
Vasya is currently working as a clerk. He wants to sell a ticket to every single person in this line.
Can Vasya sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?
Return YES, if Vasya can sell a ticket to each person and give the change. Otherwise return NO.
I found that the code works for ALL tests if I swap the check for amount50 >= 1 and amount25 >= 1 with the amount25 >= 3 but I am not sure WHY this works.
function tickets(peopleInLine){
let amount25 = 0;
let amount50 = 0;
let amount100 = 0;
for(let i = 0; i < peopleInLine.length; i++){
if(peopleInLine[i] === 100){
if(amount25 >= 3){
amount25 -= 3;
amount100++;
}else if(amount25 >= 1 && amount50 >= 1){
amount25 -= 1;
amount50 -= 1;
amount100++;
}else{
return "NO";
}
}
if(peopleInLine[i] === 50){
if(amount25 >= 1){
amount25--;
amount50++;
} else {
return "NO";
}
}
if(peopleInLine[i] === 25){
amount25++;
}
}
return "YES";
}
On Codewars you can put console.log statements (or equivalent statements in languages other than JavaScript) in your code to print the input parameters or other variables when you're having trouble figuring out what is going wrong in your code or with your assumptions about the input. I just did this with your code and saw that the test your code is failing on is [ 25, 25, 25, 25, 50, 100, 50 ], so that should show you exactly why your code would be failing when in response to getting a $100 you first try to return three $25s as change instead of checking for a $50 and $25 first -- you receive four $25s, one of which you give as change to the first person with a $50, but then because you give the remaining three $25s (rather than the $50 and one $25) as change to the person with the $100, you no longer have a $25 to make change for the last person's $50.
I found a nice simple bit of code to show a count up and I can easily vary the steps each count up makes. So this one counts up in 10's so 10, 20, 30, 40, 50, etc. appears on the page.
Code text is as follows
<script type="text/javascript">
var counter = 0
var timer; { counter = counter + 10;//increment the counter by 10
//display the new value in the div
document.getElementById("timer_container").innerHTML = counter;
}
</script>
</head>
<body onload='timer=setInterval("countUP()", 1000 );'>
<div id="timer_container"<0>/div>
</body>
</html>
What beats me however is how to include the counter multiple times on a single page with each counter using a different counting amount. I'd like to be able to include a lot of counters in a table on a page with each one going up in different amounts 1, 2, 3, 4 etc.
What this is for is to help my teacher wife with times tables on a big whiteboard so there would be 12 different colored areas on display featuring each times table from 1 to 12
Each colored box displayed will include:
image - mysite.com/3timestableicon.png
text - "3x Table"
counter code - going up in increments of 3 and stopping after 20 steps
text- "See - Each step is in 3's!"
I can probably figure out the table and all the different cells to make all this display correctly but I'm stuck getting more than one counter to appear on a single page at the moment so would appreciate any help on this bit.
The problem you have is that var counter = 0 is a global variable. So if you copy n paste the script multiple times, you're sharing the same variable.
I'm not sure how much programming experience you have, but the best way to do this would be to have a counting function. Your code snippet looks like you incorrectly pasted it, but you need to have a function, say count that increments a counter and wraps it when it gets to 12 (or however high you want to go) before looping around. Remove the if statement if you don't want it to loop.
var k = 0;
function count() {
if (k == 12) k = 0;
else k = k + 1;
// update your divs now with the new value of k
}
Then you can execute that with the interval function to count every x number of millis.
<body onload='timer=setInterval("count()", 1000 );'>
Then have an update function which updates the div's with the new values. And this function would apply a multiplier to the current counter (k) value.
function updateDisplay(id, multiplier) {
document.getElementById(id).innerHTML = k * multiplier;
}
Then you can wrap all that together for the times table of 1 to 12 by using this code in the count function to update all the div containers.
for (var n = 1; n <= 12; n++) {
updateDisplay("timer_container_" + n, n);
}
In your HTML you'd just add in 12 divs, each one with the id, "timer_container_1" etc.
I'll leave it as an exercise to the reader to put these functions together and the required divs into a HTML file.
I have this code to solve a 3x3 sudoku (Only rows/columns are need to be checked, there can be 4 of the same numbers in one block, for example this is a good one:
123
231
312
)
I have the function fillsquare, with input an array [[1,2,0],[0,3,1],[3,0,2]] where the 0 need the be changed to the right number.
function fillsquare(square){
for (var a = 0; a<4; a++){
for (var b = 0; b<4; b++){
if (square[a][b] === 0){
// do the function to check whether I can enter the right number
}
else {
// ?????????????????????
}
}
}
}
According to my professor, it should be a recursive code, but I dont think this is recursive, anyways this base will work.
I still need to change the 0's to the number that fits in place.
I have a small clue how to do this with bruteforce/backtracking, but no code to achieve this.
enter 1 in the zero, if there isn't a one in the row or column, continue to the next zero. Else enter 2 and check again etc.
I hope you can help!
PS: My prof. gave me this psuedocode for checking etc.:
function fillSquare(square) {
find first 0 in square
if (there is no 0) {done!}
else {determine every value that can be put on the place with the zero
(check column and row) save them for example in a temprary row
for (every possible value) {
fillSquare(square with value filled in place)
}
}
}
I hope this is a bit clear, it is translated;)