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 trialArmin Kadic
16,242 PointsIs one loop preferred over the other?
Since I did my loop like the first one shown here, I wonder if it's bad practice and if I should be using the second one instead? I usually use the second one, but the first one seemed better now (or not). However, it gives the same outcome.
function createDeck() {
var suites = ['♠︎','♣︎','♥︎','♦︎'];
var ranks = ['Ace','King','Queen','Jack','10','9','8','7','6','5','4', '3','2'];
var deck = [];
LOOP 1
for ( const suitesElement of suites ) {
for ( const ranksElement of ranks ) {
let card = [ranksElement, suitesElement];
deck.push(card);
}
}
return shuffle(deck);
}
OR LOOP 2
for (let i=0; i<suites.length; i++) {
for (let j=0; j<ranks.length; j++) {
let card = [];
card.push(ranks[j], suites[i]);
deck.push(card);
}
}
return shuffle(deck);
}
1 Answer
Amber Stevens
Treehouse Project ReviewerOne of the greatest things about coding is that there are often multiple ways to solve a problem. From what I see here I can't see that it would make much of a difference whether you used the first loop example or the second. I personally prefer the first one as well because with all those i's and j's inside of square brackets I feel like it can look slightly more confusing/messy. However either way is acceptable!
Armin Kadic
16,242 PointsArmin Kadic
16,242 PointsAwesome, thank you for the answer!