more memories

master
Thom Page 10 years ago
parent fe1a7cf3a9
commit efe9901d12

@ -2,24 +2,23 @@ window.onload = function() {
console.log('loaded'); console.log('loaded');
// Invoke your chain of functions and listeners within window.onload // Invoke your chain of functions and listeners within window.onload
// var button = document.getElementsByTagName('button');
// button.onclick(function(){ var startButton = document.querySelector('button');
// start(); startButton.onclick = function() {
// }) start();
start(); }
} }
var tiles = ['A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'E', 'E'];
// USE THIS TO SHUFFLE YOUR ARRAYS // USE THIS TO SHUFFLE YOUR ARRAYS
//o=array //o = array
var tiles = ['A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'E', 'E'];
function shuffle(o) { 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); for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o; return o;
}; };
function start(){ function start(){
shuffle(tiles); shuffle(tiles);
makeAndDisplayTiles(); makeAndDisplayTiles();
} }
@ -27,44 +26,52 @@ function start(){
function makeAndDisplayTiles(){ function makeAndDisplayTiles(){
document.getElementById('game').innerHTML = ""; document.getElementById('game').innerHTML = "";
document.getElementById('info').innerHTML = ""; document.getElementById('info').innerHTML = "";
for(var i = 0; i<tiles.length;i++){ for(var i = 0; i < tiles.length; i++){
var tile = document.createElement('div'); var tile = document.createElement('div');
tile.setAttribute('class', 'column'); tile.className = "column";
tile.setAttribute('data-value',tiles[i]); tile.setAttribute('data-value', tiles[i]);
document.getElementById('game').appendChild(tile); document.getElementById('game').appendChild(tile);
tile.onclick = function(){ tile.onclick = function(){
makePlay(this); makePlay(this);
}; };
} }
// addEventsToTiles();
} }
// function addEventsToTiles(){
// var tile = document.getElementsByClassName('column');
// tile.onclick = makePlay(this);
// }
function makePlay(tile){ function makePlay(tile){
tile.innerHTML = tile.dataset.value; tile.innerHTML = tile.dataset.value;
tile.className += " found"; tile.className += " clicked";
}
var selected = document.getElementsByClassName('clicked');
if (selected.length === 2) {
checkForMatch();
}
}
function processData(n) { function checkForMatch() {
var result=""; setTimeout(function() {
for(var i = 1; i<=n; i++){ var selected = document.getElementsByClassName('clicked');
var spaces = i; if (selected[0].innerText === selected[1].innerText) {
while(spaces <= n-1){ console.log('match');
result+=" "; selected[0].className = "column found";
spaces++; selected[0].className = "column found";
} else {
console.log('no match');
selected[0].innerText = "";
selected[1].innerText = "";
selected[0].className = "column";
selected[0].className = "column";
} }
var stairs = i; checkForWin();
while(stairs > 0){ }, 1500);
result+='#'; }
stairs--;
function checkForWin() {
var foundItems = document.getElementsByClassName('found');
if (foundItems.length === 10) {
for (var i=0; i < foundItems.length; i++) {
foundItems[i].className = "column won";
} }
result+='\n'
} }
console.log(result);
} }

@ -2,24 +2,23 @@ window.onload = function() {
console.log('loaded'); console.log('loaded');
// Invoke your chain of functions and listeners within window.onload // Invoke your chain of functions and listeners within window.onload
// var button = document.getElementsByTagName('button');
// button.onclick(function(){ var startButton = document.querySelector('button');
// start(); startButton.onclick = function() {
// }) start();
start(); }
} }
var tiles = ['A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'E', 'E'];
// USE THIS TO SHUFFLE YOUR ARRAYS // USE THIS TO SHUFFLE YOUR ARRAYS
//o=array //o = array
var tiles = ['A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'E', 'E'];
function shuffle(o) { 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); for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o; return o;
}; };
function start(){ function start(){
shuffle(tiles); shuffle(tiles);
makeAndDisplayTiles(); makeAndDisplayTiles();
} }
@ -27,44 +26,73 @@ function start(){
function makeAndDisplayTiles(){ function makeAndDisplayTiles(){
document.getElementById('game').innerHTML = ""; document.getElementById('game').innerHTML = "";
document.getElementById('info').innerHTML = ""; document.getElementById('info').innerHTML = "";
for(var i = 0; i<tiles.length;i++){ for(var i = 0; i < tiles.length; i++){
var tile = document.createElement('div'); var tile = document.createElement('div');
tile.setAttribute('class', 'column'); tile.className = "column";
tile.setAttribute('data-value',tiles[i]); tile.setAttribute('data-value', tiles[i]);
document.getElementById('game').appendChild(tile); document.getElementById('game').appendChild(tile);
tile.onclick = function(){ tile.onclick = function(){
makePlay(this); makePlay(this);
}; };
} }
// addEventsToTiles();
} }
// function addEventsToTiles(){
// var tile = document.getElementsByClassName('column');
// tile.onclick = makePlay(this);
// }
function makePlay(tile){ function makePlay(tile){
tile.innerHTML = tile.dataset.value; tile.innerHTML = tile.dataset.value;
tile.className += " found"; tile.className += " clicked";
}
var selected = document.getElementsByClassName('clicked');
if (selected.length === 2) {
checkForMatch();
}
}
function processData(n) { function checkForMatch() {
var result=""; setTimeout(function() {
for(var i = 1; i<=n; i++){ var selected = document.getElementsByClassName('clicked');
var spaces = i; if (selected[0].innerText === selected[1].innerText) {
while(spaces <= n-1){ console.log('match');
result+=" "; selected[0].className = "column found";
spaces++; selected[0].className = "column found";
} else {
console.log('no match');
selected[0].innerText = "";
selected[1].innerText = "";
selected[0].className = "column";
selected[0].className = "column";
} }
var stairs = i; checkForWin();
while(stairs > 0){ }, 1500);
result+='#'; }
stairs--;
function checkForWin() {
var foundItems = document.getElementsByClassName('found');
if (foundItems.length === 10) {
for (var i=0; i < foundItems.length; i++) {
foundItems[i].className = "column won";
} }
result+='\n'
} }
console.log(result);
} }
// don't need data value
// 'found' is only added after a match
// checkForMatch goes inside a setTimeout
// start should begin with a listener
// 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',
//
// do not need to turn off the event listener
// apply 'won' class

Loading…
Cancel
Save