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
Jose Villegas
Full Stack JavaScript Techdegree Student 8,278 Pointshi Im working on my memory game I got a few thing to fix i need help
https://github.com/jvillegas0831/Memory-Game-
I need to make the cards shuffle every time the restart button is click or the game is over,
make a modal box when the game is over hope somebody can help thanks
1 Answer
Torben Korb
Front End Web Development Techdegree Graduate 91,433 PointsHi Jose, first of all nice project! Projects like this help to improve problem solving in code a lot. By myself I created a memory game in different versions and progress on my coding experience, see following. Maybe they can be an inspiration to your project.
http://noah.teamdigitalcreative.com/
http://torbenkorb.github.io/memory-game/
Back to your problem. The best would be to create a copy of your initial array on each start of the game and shuffle this new array. There are several methods to achieve this. One of the elegant approaches I found to be something like this:
// Take an array as parameter
// create a new array of arrays with the structure [random no, value]
// sort the array by the first entry (which is the random no)
// output a new array with just the sorted values
const shuffleArray = arr => arr
.map(item => [Math.random(), item])
.sort((a, b) => a[0] - b[0])
.map(list => list[1]);
Then you could initialize your game like this:
function init() {
//Cards
const cards = shuffleArray(deck);
for (let i = 0; i < deck.length; i++) {
const card = document.createElement("li");
card.classList.add("card");
card.innerHTML = `<i class="${card[i]}"></i>`;
cardsContainer.appendChild(card);
//Add Click Event
click(card);
}
}
The modal box for the end of the game could be created as hidden screen. At the end of the game instead of alerting you could display this screen. Simply style your box with CSS and set it to display: none. Instead of alert set the display property of this box to 'block'. Example:
document.querySelector('.YOUR_BOX_CLASS').style.display = 'block'
Hope this helps, good luck!
Jose Villegas
Full Stack JavaScript Techdegree Student 8,278 PointsJose Villegas
Full Stack JavaScript Techdegree Student 8,278 PointsThanks! appreciate your time and help