master
Thom Page 10 years ago
parent 627cbb3154
commit 0c3402d1c8

@ -1,17 +1,17 @@
window.onload = function() {
console.log('loaded');
// Invoke your chain of functions and listeners within window.onload
// When the start button is clicked, the start() function is invoked
var startButton = document.querySelector('button');
startButton.onclick = function() {
start();
}
}
var tiles = ['A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'E', 'E'];
// Make your array of letters for your tiles
// USE THIS TO SHUFFLE YOUR ARRAYS
// Use this function later to shuffle your array of tiles
//o = array
function shuffle(o) {
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
@ -19,80 +19,68 @@ function shuffle(o) {
};
function start(){
shuffle(tiles);
makeAndDisplayTiles();
// shuffle your tiles
// invoke the makeAndDisplayTiles function
}
function makeAndDisplayTiles(){
document.getElementById('game').innerHTML = "";
document.getElementById('info').innerHTML = "";
for(var i = 0; i < tiles.length; i++){
var tile = document.createElement('div');
tile.className = "column";
tile.setAttribute('data-value', tiles[i]);
document.getElementById('game').appendChild(tile);
tile.onclick = function(){
makePlay(this);
};
}
// this function should clear the game (when the player presses the 'start' button,
// this function is invoked, clearing the game board)
// write a loop that iterates over the tiles array and creates each tile (a div)
// -each tile should have a class of 'column'
// -each tile should have a 'data-value' corresponding to its letter
// -append each tile to the game
// -give each tile an event listener that invokes the makePlay(tile) function when
// the tile is clicked
}
function makePlay(tile){
tile.innerHTML = tile.dataset.value;
tile.className += " clicked";
var selected = document.getElementsByClassName('clicked');
// display the letter inside the tile
// give the tile a class of 'clicked'
if (selected.length === 2) {
checkForMatch();
}
// if there are two tiles with the class 'clicked', run the checkForMatch function
}
function checkForMatch() {
setTimeout(function() {
var selected = document.getElementsByClassName('clicked');
if (selected[0].innerText === selected[1].innerText) {
console.log('match');
selected[0].className = "column found";
selected[0].className = "column found";
} else {
console.log('no match');
selected[0].innerText = "";
selected[1].innerText = "";
selected[0].className = "column";
selected[0].className = "column";
}
checkForWin();
}, 1500);
}
function checkForWin() {
var foundItems = document.getElementsByClassName('found');
// write all your code for this function inside setTimeout below:
if (foundItems.length === 10) {
for (var i=0; i < foundItems.length; i++) {
foundItems[i].className = "column won";
}
}
}
setTimeout(function() {
// write your code here
// don't need data value
// 'found' is only added after a match
// checkForMatch goes inside a setTimeout
// start should begin with a listener
// if two tiles match:
// -add a class of 'found' to each
// -remove the class of 'clicked' from each
// -remember, if you remove the 'column' class, the tile will disappear
// (and you don't want that)
// -run the checkForWin function
// remove a class name by using 'className' and writing the names
// of the classes you want to keep, and adding classes if you want
// EXAMPLE: If an element with the class of 'column' also has a class of
// 'clicked' and you want to remove 'clicked',
//
// if your two tiles DON'T match
// - remove the innerText so the letter goes away
// - remove the class of 'clicked' (but keep the class of 'column')
// do not need to turn off the event listener
// apply 'won' class
// HINTS
// Remove a class by using 'className' and writing the names
// of the classes you want to keep. EXAMPLE: If an element with the class of
// 'column' also has a class of 'clicked' and you want to remove 'clicked',
// just say className = "column", and 'clicked' will be removed. You can also
// add classes in the same way, while omitting others.
// Note that if you change a class, the list of elements with that className
// will change in real time (the nodeList updates). You'll have to figure out a
// way around this to avoid getting errors
}, 1500);
}
function checkForWin() {
// if all tiles have a class of 'found', the player has won
// in which case, add a class of 'won' to each tile
}

Loading…
Cancel
Save