0

This code doesn't work, and seems to make document.write stop working

I'm trying to make an array of a deck of cards the easy way, but it's doesn't work, and not only that, if you go to a lesson that has document.write, it will apparently disable it. I've tried reporting the glitch, but no reply has come back. var preCards = ['A','2','3','4','5','6','7','8','9','10','J','Q','K']; //makes each value for the cards var spades=[]; while (spades.length != 13) { spades = preCards[spades.length] + "♠" } var hearts=[]; while (hearts.length != 13) { hearts = preCards[hearts.length] + "♥" } var clubs=[]; while (clubs.length != 13) { clubs = preCards[clubs.length] + "♣" } var diamonds=[]; while (diamonds.length != 13) { diamonds = preCards[diamonds.length] + "♦" } //took the card values and added a suit to it var cards= spades.concat(hearts, clubs, diamonds) //combines all cards into one full deck document.write(cards);

30th Oct 2018, 2:32 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
7 Respuestas
+ 1
You're hanging the main thread in an infinite loop. spades.push( preCards[spades.length] + "♠") will append to the array and let your code move on to the next...infinite loop :)
30th Oct 2018, 4:12 PM
Kirk Schafer
Kirk Schafer - avatar
+ 1
well it worked, I'll need to see where it taught the .push because I don't remember it in the JavaScript course.
30th Oct 2018, 5:06 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
+ 1
oh, so what mine does is replacing the one element instead of adding, so it always stays at one. that makes sense now
30th Oct 2018, 6:06 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
+ 1
Well...there's actually a lot more you can do and it usually comes down to search (whether you see something in another code here--like push()--or you wish you could do something better, if only there was a function for that (there usually is)). To learn more / even what functions to be wanting, you could find someone with a lot of codes and start asking about their functions or...try another language course. Design patterns repeat and if one language has a "substring" function (to extract part of a string), then it's very likely all languages have one (and they'll have similar names). Then, you can search for "how can I [do in x] like I do in [language y]?" There's more in custom lessons here (e.g., ES6 is actually Javascript 6), W3Schools is infamous for documenting HTML/CSS/JS features in an approachable way, Mozilla (devs for Firefox) has excellent but more technical docs and a lot of things come up in Q&A (or just out on Google searches for javascript features).
31st Oct 2018, 2:43 AM
Kirk Schafer
Kirk Schafer - avatar
0
shouldn't the while loop prevent that once it gets to 13 cards of a suit
30th Oct 2018, 4:59 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
0
You're only ever getting 1 element in each of these, because you're retrieving 1 element in your while loops and then setting: whole_array = that_one_element // so it never increases past 1 I think...they don't show push() in the lessons here (you have to find that by searching). To use the pattern you started on, you could do it this way: spades[spades.length] = preCards[spades.length] + ... Which works for me because JavaScript will add the new element for you. This is not something you should rely on in all languages though (because it usually requires memory to be allocated first, which is why functions like push() or append() are preferred. Javascript is just forgiving to this syntax -- it's fine, but just FYI).
30th Oct 2018, 5:49 PM
Kirk Schafer
Kirk Schafer - avatar
0
so if that's not taught in the course, what else isn't, because I think a lot of the built in functions could help
30th Oct 2018, 6:08 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar