-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
192 lines (154 loc) · 5.72 KB
/
Copy pathapp.js
File metadata and controls
192 lines (154 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// To-do:
// button transform in css
// Have a deck which will be an array, that will hold objects, the objects will be the cards and have a front / back property.
// -CM
// Have a button to click for next card, which will randomly choose front or back of a different card.
// -CM
// Some implementation for showing the answer which will display the opposite side of the card. - Easy fix, just display both front and back, by calling both key properties for the card object.
// -CM *Note* : Update so front / back displays at their respective location.
// Save card button will create a new object within the deck and then it will save the front of card text to the "front" value, and the back of card text to the 'back' value. -CM
// Functionality needs to be made to push a new card onto the stack. It will push an object onto the deck array.
// -CM
// Use logic to show either front or back randomly, then when show answer is clicked, display both.
// -CM
// Tell user their card has been saved somehow... Button transformation with styling to show button has been clicked, find from the 10 javascript projects with florinpop.
// This is the array that holds the cards
let deck =[]
// CurrentCard array keeps track of the current card, necessary to provide answer when "show answer" is clicked.
let currentCard=[]
// Front text for new card
const cardFrontText= document.querySelector('#cardFrontText')
const BURP='Blargh!'
// Back text for new card
const cardBackText= document.querySelector('#cardBackText')
// Front card content location:
const front = document.querySelector('#frontDisplay')
// Back card content location:
const back = document.querySelector('#backDisplay')
// Save button
const saveButton = document.querySelector('#saveButton')
// Show answer button
const showAnswerButton = document.querySelector('#showAnswer')
// Next card button
const nextCardbttn = document.querySelector('#nextCard')
// Modal selector and buttons
const showModal = document.querySelector('#showModal')
const modal = document.querySelector('#newCardModal')
const closeModal = document.querySelector('#closeModal')
showModal.addEventListener('click', () => {
modal.showModal()
})
closeModal.addEventListener('click', () => {
modal.close();
})
// check localStorage for a deck, if it has one then dump it into the deck array just created
if (localStorage.getItem('localDeck') != null) {
deck.push(...(JSON.parse(localStorage.getItem('localDeck'))))
}
// if it doesn't have a deck in localStorage, create it with the contents of deck
else {
localStorage.setItem('localDeck', JSON.stringify(deck))
}
// Factory function to return an object with a front / back value
function newCard (front, back) {
return {
front, back
}}
// Added event listener to document for going to next card and for showing answer
document.addEventListener('keydown', (event) => {
let name = event.key;
let code = event.code;
if (name === 'ArrowRight') {
console.log('Right arrow pressed');
nextCard();
console.log('Next card delivered')
}
if (name ===' ') {
console.log(code)
try { answerPlease()}
catch{
console.log('An error ocurred has ocurred retrieving answer from keyEvent') }
console.log('Answer delivered')
}
})
// this saves the deck as a localDeck item to localStorage,
function saveToLocalStorage() {
localStorage.setItem('localDeck',JSON.stringify(deck))
}
// Do this function on click event for save card button.
// deck.push(newCard(frontContent, backContent))
saveButton.addEventListener('click', pushCard => {
if (cardFrontText.value ==='' || cardBackText.value ==='') {
alert('Please enter card front and back values')}
else{
deck.push(newCard(cardFrontText.value, cardBackText.value))
console.log('clicked')
saveToLocalStorage()
console.log('Current deck saved to Local Storage')}
textInputReset()
})
modal.addEventListener('keyDown', (event) => {
let name =event.key;
let code =event.code;
if (name === 'Enter') {
console.log('enter key pressed')
}
})
// This randomly selects front / back in the object.
function cardSide() {
if (Math.random() <0.5) {
return 'front' }
else { return 'back' }
}
// Resets the text input
function textInputReset() {
cardFrontText.value='';
cardBackText.value='';
}
// Function that provides the next card on click.
function nextCard () {
front.innerText='?'
back.innerText='?'
if (deck.length===0) {
alert('Please create a new deck to begin')
console.log('Error, no deck found')
return
}
let coin=cardSide()
console.log(coin)
// Set to empty string to make answer hidden, this is overwritten with a value provided within the function further down
if (coin ==='front') {
return (front.innerText = function() {
let count = Math.floor((Math.random()*deck.length-1))+1
console.log('front works')
currentCard.pop()
console.log(count)
currentCard.push(deck[count])
console.log(currentCard)
setTimeout(textInputReset(), 1000)
return deck[count][coin]
}())}
else {
return back.innerText = function() {
let count = Math.floor((Math.random()*deck.length-1))+1
console.log('back works')
currentCard.pop()
console.log(count)
currentCard.push(deck[count])
console.log(currentCard, 'current card')
setTimeout(textInputReset(), 1000)
return deck[count][coin]
}()}
}
// Displays the answer
function answerPlease () {
try {
frontDisplay.innerText=currentCard[0].front;
backDisplay.innerText=currentCard[0].back;
currentCard.pop()}
catch (err){
console.log(err, 'No current Card value saved')
}
}
nextCardbttn.addEventListener('click', nextCard)
showAnswerButton.addEventListener('click', answerPlease)