1 class WordMemory extends WordQuiz {
3 let target = click.currentTarget;
4 if (!target.classList.contains('turn')) {
6 this.turned.push(target);
8 this.log('pick', target.id, target.index);
10 else if (this.turned.length < 2) {
14 if (this.turned.length <= 1) {
15 return; // first choice
19 let match = !this.pairs ? this.turned[0].id == this.turned[1].id : (
20 this.pairs[this.turned[0].id] == this.turned[1].id
21 || this.pairs[this.turned[1].id] == this.turned[0].id
23 if (!match && !this.turned[0].classList.contains('bad')) {
24 put(this.turned[0], '.bad'); // indicate failure on first card
29 // lock both as correct
30 this.turned.forEach(card => put(card, '.good![onclick]'));
32 if (Array.from(this.form.children).every(card => card.classList.contains('good'))) {
33 put(this.form, '.good');
39 // fold back earlier cards
40 this.turned.splice(0, 2)
41 .forEach(card => put(card, '!.turn!.bad'));
49 this.dataurl = '/data/wordpairs.json';
50 fetch(this.dataurl).then(res => res.json()).then(pairs => {
59 this.form = document.getElementById('quiz');
63 const aspect = this.form.clientWidth / window.innerHeight;
66 let cols = Math.round(Math.sqrt(count) * aspect**.5);
67 count = cols * Math.ceil(count / cols);
68 this.form.style['grid-template-columns'] = `repeat(${cols}, 1fr)`;
69 cards = this.words.splice(0, count>>1).map(row => row[2]);
70 cards.push(...cards.map(val => -val));
73 cards = Object.entries(this.pairs).flat()
74 .map(e => e.toString())
77 cards.shuffle().forEach((word, seq) => {
78 let ref = Math.abs(word);
80 'figure>img[src=$]<', `/data/word/32/${ref}.jpg`, {
81 onclick: e => this.turn(e),
83 className: word < 0 ? 'mirror' : '',