Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Edward campbell malan van wyk
6,403 PointsFor loop not incrementing inside function
Hi,
Can anyone see a reason why the first forloop in the below function would not increment, becuase the console. log at the bottom display the value of i 4 times but all as 0.
function generateHand(playerHand) { for (var i = 0; i < 4; i++) {
//For the purpose of this assesment each player and Dealer will get a random number of cards not less than 2 and not exceeding 5
noOfCards = Math.floor((Math.random() * 4) + 2);
for (var j = 0; j < noOfCards; j++)
{
totalvalue[i] = calculateHandTotal(cardValue);
playerHand += card(cardValue,cardSuit) + " ";
cardValue = Math.floor((Math.random() * 13) + 1);
cardSuit = Math.floor((Math.random() * 4) + 1);
}
console.log(i +" "+ totalvalue[i]);
return playerHand;
}
}
regards.
2 Answers
Oliver Duncan
16,642 PointsLooks to me like you're declaring the variable 'j' in the for loop, then using 'i' in the body of the loop.
Edward campbell malan van wyk
6,403 Pointsyes sorry it seems the block above did not copy the code correctly. I am using two for loops.
function generateHand(playerHand) { for (var i = 0; counter < 4; i+=1) { //For the purpose of this assesment each player and Dealer will get a random number of cards not less than 2 and not exceeding 5 noOfCards = Math.floor((Math.random() * 4) + 2);
for (var j = 0; j < noOfCards; j += 1)
{
totalvalue[i] = calculateHandTotal(cardValue);
playerHand += card(cardValue, cardSuit) + " ";
cardValue = Math.floor((Math.random() * 13) + 1);
cardSuit = Math.floor((Math.random() * 4) + 1);
}
console.log(i);
return playerHand;
}
}
it seems the first for loop is looping 4 times but the i variable always stays 0.
Edward campbell malan van wyk
6,403 Pointsfor some reason the code is not copying correctly.
Oliver Duncan
16,642 PointsTo properly show code, refer to the Markdown Cheatsheet. You need a linebreak, then three ticks (```) and the name of the language.
So let's take a look:
function generateHand(playerHand) {
for (var i = 0; counter < 4; i+=1) { // You use counter here instead of i?
noOfCards = Math.floor((Math.random() * 4) + 2);
for (var j = 0; j < noOfCards; j += 1) {
totalvalue[i] = calculateHandTotal(cardValue);
playerHand += card(cardValue, cardSuit) + " ";
cardValue = Math.floor((Math.random() * 13) + 1);
cardSuit = Math.floor((Math.random() * 4) + 1);
}
console.log(i);
return playerHand;
}
Shouldn't your first for loop start off (var i = 0; i < 4; i+=1)? You're using a variable 'counter'.