Please help!!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nasren Jak
    New Member
    • Apr 2018
    • 1

    Please help!!!

    Hi, I am very new in coding and I need to submit this assignment in 3 hours. It's a memory game and I did all the required JS coding but I can't figure out how to put a alert pop-up when the game ends with a congratulations message and a restart option (it doesn't need to say restart, when clicking OK it should create a new game, that's all). I am about to fail this class and any help greatly appreciated. I put all my js coding below.

    Code:
    
    const $controls = document.querySelector(".controls");
    const $controlBtns = $controls.children;
    const $game = document.querySelector(".game");
    
    const status = {
        level: undefined,
        arrAllLetters: ["A", "B", "C", "D", "E", "F"],
        arrSelectedLetters: [],
        pair: [],
        symbols: [],
        guessed: []
    };
    
    
    function resetGame() {
        status.level = undefined;
        status.arrSelectedLetters = [];
        status.guessed = [];
    }
    
    
    function shuffleArray(array) {
        for (let x = array.length - 1; x > 0; x--) {
            let holder = Math.floor(Math.random() * array.length);
            var itemValue = array[x];
            array[x] = array[holder];
            array[holder] = itemValue;
        }
        return array;
    }
    
    // 
    function flipCard(ev) {
        let clickedCard = ev.currentTarget;
        let letter = clickedCard.lastElementChild.firstElementChild.textContent;
        
        clickedCard.classList.add("flip");
        
        // check if pair doesn't contain the clicked element
        if (!status.pair.includes(clickedCard)) {
            status.pair.push(clickedCard);
            status.symbols.push(letter);
            
            // check if you have 2 elements in the pair
            if (status.pair.length === 2) {
                
                // compare letters
                if (status.symbols[0] === status.symbols[1]) {
                    
                    // match found
                    status.guessed.push(status.symbols[0]);
                    
                    status.pair[0].removeEventListener("click", flipCard);
                    status.pair[1].removeEventListener("click", flipCard);
                    
                    // if the number of guessed cards is equal to the number of 
                    // arrSelectedLetters, there is nothing left to guess - game over
                    if (status.guessed.length === status.arrSelectedLetters.length) {
                        
                        let timer_3 = window.setTimeout(function () {
                            
                            // reset the status object
                            resetGame();
                            
                            // remove all cards from your game div
                            $game.innerHTML = "";
                            
                            // reveal the control panel again
                            $controls.classList.remove("hide-content");
                            
                            window.clearTimeout(timer_3);
                        }, 1000);
                    }
                    
                } else {
                    
                    // unflip the cards - needs to be delayed
                    let timer_1 = window.setTimeout(function () {
                        status.pair[0].classList.remove("flip");
                        status.pair[1].classList.remove("flip");
                        window.clearTimeout(timer_1);
                    }, 1000);
                }
                
                // empty pair and symbols after comparison
                // make it ready for the next comparison
                let timer_2 = window.setTimeout(function () {
                    status.pair = [];
                    status.symbols = [];
                    window.clearTimeout(timer_2);
                }, 1000);
            }
        }
        
        console.log(status);
    }
    
    
    function createElements() {
    
        // append fragment to the DOM only once!
        const fragment = document.createDocumentFragment();
        
        // display each card twice
        for (let i = 0; i < 2; i++) {
            
            status.arrSelectedLetters = shuffleArray(status.arrSelectedLetters);
            
            // create your cards
            for (let j = 0; j < status.level; j++) {
                
                // create your elements here
                const cardHolder = document.createElement("div");
                cardHolder.classList.add("card-holder");
                
                const card = document.createElement("div");
                card.classList.add("card");
                
                const front = document.createElement("div");
                front.classList.add("front", "side");
                
                const back = document.createElement("div");
                back.classList.add("back", "side");
                
                const span = document.createElement("span");
                span.textContent = status.arrSelectedLetters[j];
                
                
                // card structure created here:
                back.appendChild(span);
                card.appendChild(front);
                card.appendChild(back);
                cardHolder.appendChild(card);
                fragment.appendChild(cardHolder);
                
                card.addEventListener("click", flipCard);
            }
        }
        
        // append the entire set of cards to the DOM
        $game.appendChild(fragment);
    }
    
    // choose the level to play
    function chooseLevel(ev) {
        
        // save the number of cards
        status.level = parseInt(ev.target.dataset.level);
        
        // hide your controls here
        $controls.classList.add("hide-content");
        
        // prepare your array
        for (let i = 0; i < status.level; i++) {
            status.arrSelectedLetters.push(status.arrAllLetters[i])
        }
        
        // create cards
        createElements();
    }
    for (let b of $controlBtns) {
        b.addEventListener("click", chooseLevel);
    }
Working...