Skip to content

added previous button, binding, and function #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions public/dist/js/bundle.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/dist/js/bundle.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h1 class="site-heading">Practice JavaScript!</h1>
<div class="problem-group">
<div id="problem" class="problem">&zwnj;</div>
<div class="controls">
<!--<img id="prev-problem" class="prev-problem" src="./dist/img/previous.svg" alt="Previous problem" height="80">-->
<img id="prev-problem" class="prev-problem" src="./dist/img/previous.svg" alt="Previous problem" height="80">
<img id="shuffle-problems" class="shuffle-problems" src="./dist/img/shuffle.svg" alt="Shuffle problems" title="Shuffle problems" height="80">
<img id="next-problem" class="next-problem" src="./dist/img/next.svg" alt="Next problem" title="Next problem"height="80">
</div>
Expand Down
27 changes: 24 additions & 3 deletions src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
const evalConsoleEl = document.getElementById('eval-output');
const assertConsoleEl = document.getElementById('assert-output');
const shuffleProblemsButtonEl = document.getElementById('shuffle-problems');
const previousProblemButtonEl = document.getElementById('prev-problem');
const nextProblemButtonEl = document.getElementById('next-problem');

// get indexes
Expand All @@ -79,6 +80,18 @@
return ind;
}

function getPreviousIndex(problemsArr) {
let probInd;
const currentIndex = config.currentIndex;
// if at beginning, go to end
if (currentIndex === 0) {
probInd = problemsArr.length - 1;
} else {
probInd = currentIndex - 1;
}
return probInd;
}

function getNextIndex(problemsArr) {
let probInd;
const currentIndex = config.currentIndex;
Expand All @@ -96,15 +109,22 @@
return problemsArr[config.currentIndex];
}

function previousProblem(e) {
console.log('previousProblem!');
config.currentIndex = config.shuffle
? getRandomIndex(problems)
: getPreviousIndex(problems);
updateLocalstore(config).then(_ => {
window.location.reload();
});
}

function nextProblem(e) {
console.log('nextProblem!');
config.currentIndex = config.shuffle
? getRandomIndex(problems)
: getNextIndex(problems);
console.log('config.currentIndex:', config.currentIndex);
updateLocalstore(config).then(_ => {
console.log('then reload!');
// loadProblem(currentProblem, true);
window.location.reload();
});
}
Expand Down Expand Up @@ -282,6 +302,7 @@
}
});
shuffleProblemsButtonEl.addEventListener('click', toggleShuffle);
previousProblemButtonEl.addEventListener('click', previousProblem);
nextProblemButtonEl.addEventListener('click', nextProblem);

// start it up!
Expand Down