forked from bradtraversy/vanillawebprojects
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
76c37b8
commit 27002c2
Showing
4 changed files
with
456 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge" /> | ||
<link | ||
rel="stylesheet" | ||
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css" | ||
integrity="sha256-+N4/V/SbAFiW1MPBCXnfnP9QSN3+Keu+NlB+0ev/YKQ=" | ||
crossorigin="anonymous" | ||
/> | ||
<link rel="stylesheet" href="style.css" /> | ||
<title>Memory Cards</title> | ||
</head> | ||
<body> | ||
<button id="clear" class="clear btn"> | ||
<i class="fas fa-trash"></i> Clear Cards | ||
</button> | ||
|
||
<h1> | ||
Memory Cards | ||
<button id="show" class="btn btn-small"> | ||
<i class="fas fa-plus"></i> Add New Card | ||
</button> | ||
</h1> | ||
|
||
<div id="cards-container" class="cards"> | ||
<!-- <div class="card active"> | ||
<div class="inner-card"> | ||
<div class="inner-card-front"> | ||
<p> | ||
What is PHP? | ||
</p> | ||
</div> | ||
<div class="inner-card-back"> | ||
<p> | ||
A programming language | ||
</p> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="card"> | ||
<div class="inner-card"> | ||
<div class="inner-card-front"> | ||
<p> | ||
What is PHP? | ||
</p> | ||
</div> | ||
<div class="inner-card-back"> | ||
<p> | ||
A programming language | ||
</p> | ||
</div> | ||
</div> | ||
</div> --> | ||
</div> | ||
|
||
<div class="navigation"> | ||
<button id="prev" class="nav-button"> | ||
<i class="fas fa-arrow-left"></i> | ||
</button> | ||
|
||
<p id="current"></p> | ||
|
||
<button id="next" class="nav-button"> | ||
<i class="fas fa-arrow-right"></i> | ||
</button> | ||
</div> | ||
|
||
<div id="add-container" class="add-container"> | ||
<h1> | ||
Add New Card | ||
<button id="hide" class="btn btn-small btn-ghost"> | ||
<i class="fas fa-times"></i> | ||
</button> | ||
</h1> | ||
|
||
<div class="form-group"> | ||
<label for="question">Question</label> | ||
<textarea id="question" placeholder="Enter question..."></textarea> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<label for="answer">Answer</label> | ||
<textarea id="answer" placeholder="Enter Answer..."></textarea> | ||
</div> | ||
|
||
<button id="add-card" class="btn"> | ||
<i class="fas fa-plus"></i> Add Card | ||
</button> | ||
</div> | ||
|
||
<script src="script.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
## Memory Cards | ||
|
||
Flash card app for learning. Display, add and remove memory cards with questions and answers | ||
|
||
## Project Specifications | ||
|
||
- Create flip cards using CSS | ||
- Create "Add new card" overlay with form | ||
- Display question cards and flip for answer | ||
- View prev and next cards | ||
- Add new cards to local storage | ||
- Clear all cards from local storage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
const cardsContainer = document.getElementById('cards-container'); | ||
const prevBtn = document.getElementById('prev'); | ||
const nextBtn = document.getElementById('next'); | ||
const currentEl = document.getElementById('current'); | ||
const showBtn = document.getElementById('show'); | ||
const hideBtn = document.getElementById('hide'); | ||
const questionEl = document.getElementById('question'); | ||
const answerEl = document.getElementById('answer'); | ||
const addCardBtn = document.getElementById('add-card'); | ||
const clearBtn = document.getElementById('clear'); | ||
const addContainer = document.getElementById('add-container'); | ||
|
||
// Keep track of current card | ||
let currentActiveCard = 0; | ||
|
||
// Store DOM cards | ||
const cardsEl = []; | ||
|
||
// Store card data | ||
const cardsData = getCardsData(); | ||
|
||
// const cardsData = [ | ||
// { | ||
// question: 'What must a variable begin with?', | ||
// answer: 'A letter, $ or _' | ||
// }, | ||
// { | ||
// question: 'What is a variable?', | ||
// answer: 'Container for a piece of data' | ||
// }, | ||
// { | ||
// question: 'Example of Case Sensitive Variable', | ||
// answer: 'thisIsAVariable' | ||
// } | ||
// ]; | ||
|
||
// Create all cards | ||
function createCards() { | ||
cardsData.forEach((data, index) => createCard(data, index)); | ||
} | ||
|
||
// Create a single card in DOM | ||
function createCard(data, index) { | ||
const card = document.createElement('div'); | ||
card.classList.add('card'); | ||
|
||
if (index === 0) { | ||
card.classList.add('active'); | ||
} | ||
|
||
card.innerHTML = ` | ||
<div class="inner-card"> | ||
<div class="inner-card-front"> | ||
<p> | ||
${data.question} | ||
</p> | ||
</div> | ||
<div class="inner-card-back"> | ||
<p> | ||
${data.answer} | ||
</p> | ||
</div> | ||
</div> | ||
`; | ||
|
||
card.addEventListener('click', () => card.classList.toggle('show-answer')); | ||
|
||
// Add to DOM cards | ||
cardsEl.push(card); | ||
|
||
cardsContainer.appendChild(card); | ||
|
||
updateCurrentText(); | ||
} | ||
|
||
// Show number of cards | ||
function updateCurrentText() { | ||
currentEl.innerText = `${currentActiveCard + 1}/${cardsEl.length}`; | ||
} | ||
|
||
// Get cards from local storage | ||
function getCardsData() { | ||
const cards = JSON.parse(localStorage.getItem('cards')); | ||
return cards === null ? [] : cards; | ||
} | ||
|
||
// Add card to local storage | ||
function setCardsData(cards) { | ||
localStorage.setItem('cards', JSON.stringify(cards)); | ||
window.location.reload(); | ||
} | ||
|
||
createCards(); | ||
|
||
// Event listeners | ||
|
||
// Next button | ||
nextBtn.addEventListener('click', () => { | ||
cardsEl[currentActiveCard].className = 'card left'; | ||
|
||
currentActiveCard = currentActiveCard + 1; | ||
|
||
if (currentActiveCard > cardsEl.length - 1) { | ||
currentActiveCard = cardsEl.length - 1; | ||
} | ||
|
||
cardsEl[currentActiveCard].className = 'card active'; | ||
|
||
updateCurrentText(); | ||
}); | ||
|
||
// Prev button | ||
prevBtn.addEventListener('click', () => { | ||
cardsEl[currentActiveCard].className = 'card right'; | ||
|
||
currentActiveCard = currentActiveCard - 1; | ||
|
||
if (currentActiveCard < 0) { | ||
currentActiveCard = 0; | ||
} | ||
|
||
cardsEl[currentActiveCard].className = 'card active'; | ||
|
||
updateCurrentText(); | ||
}); | ||
|
||
// Show add container | ||
showBtn.addEventListener('click', () => addContainer.classList.add('show')); | ||
// Hide add container | ||
hideBtn.addEventListener('click', () => addContainer.classList.remove('show')); | ||
|
||
// Add new card | ||
addCardBtn.addEventListener('click', () => { | ||
const question = questionEl.value; | ||
const answer = answerEl.value; | ||
|
||
if (question.trim() && answer.trim()) { | ||
const newCard = { question, answer }; | ||
|
||
createCard(newCard); | ||
|
||
questionEl.value = ''; | ||
answerEl.value = ''; | ||
|
||
addContainer.classList.remove('show'); | ||
|
||
cardsData.push(newCard); | ||
setCardsData(cardsData); | ||
} | ||
}); | ||
|
||
// Clear cards button | ||
clearBtn.addEventListener('click', () => { | ||
localStorage.clear(); | ||
cardsContainer.innerHTML = ''; | ||
window.location.reload(); | ||
}); |
Oops, something went wrong.