Skip to content

Commit 79995b0

Browse files
committed
added previous button, binding, and function
1 parent 34eef8a commit 79995b0

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

public/dist/js/bundle.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/dist/js/bundle.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ <h1 class="site-heading">Practice JavaScript!</h1>
1515
<div class="problem-group">
1616
<div id="problem" class="problem">&zwnj;</div>
1717
<div class="controls">
18-
<!--<img id="prev-problem" class="prev-problem" src="./dist/img/previous.svg" alt="Previous problem" height="80">-->
18+
<img id="prev-problem" class="prev-problem" src="./dist/img/previous.svg" alt="Previous problem" height="80">
1919
<img id="shuffle-problems" class="shuffle-problems" src="./dist/img/shuffle.svg" alt="Shuffle problems" title="Shuffle problems" height="80">
2020
<img id="next-problem" class="next-problem" src="./dist/img/next.svg" alt="Next problem" title="Next problem"height="80">
2121
</div>

src/js/index.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
const evalConsoleEl = document.getElementById('eval-output');
7070
const assertConsoleEl = document.getElementById('assert-output');
7171
const shuffleProblemsButtonEl = document.getElementById('shuffle-problems');
72+
const previousProblemButtonEl = document.getElementById('prev-problem');
7273
const nextProblemButtonEl = document.getElementById('next-problem');
7374

7475
// get indexes
@@ -79,6 +80,18 @@
7980
return ind;
8081
}
8182

83+
function getPreviousIndex(problemsArr) {
84+
let probInd;
85+
const currentIndex = config.currentIndex;
86+
// if at beginning, go to end
87+
if (currentIndex === 0) {
88+
probInd = problemsArr.length - 1;
89+
} else {
90+
probInd = currentIndex - 1;
91+
}
92+
return probInd;
93+
}
94+
8295
function getNextIndex(problemsArr) {
8396
let probInd;
8497
const currentIndex = config.currentIndex;
@@ -96,15 +109,22 @@
96109
return problemsArr[config.currentIndex];
97110
}
98111

112+
function previousProblem(e) {
113+
console.log('previousProblem!');
114+
config.currentIndex = config.shuffle
115+
? getRandomIndex(problems)
116+
: getPreviousIndex(problems);
117+
updateLocalstore(config).then(_ => {
118+
window.location.reload();
119+
});
120+
}
121+
99122
function nextProblem(e) {
100123
console.log('nextProblem!');
101124
config.currentIndex = config.shuffle
102125
? getRandomIndex(problems)
103126
: getNextIndex(problems);
104-
console.log('config.currentIndex:', config.currentIndex);
105127
updateLocalstore(config).then(_ => {
106-
console.log('then reload!');
107-
// loadProblem(currentProblem, true);
108128
window.location.reload();
109129
});
110130
}
@@ -282,6 +302,7 @@
282302
}
283303
});
284304
shuffleProblemsButtonEl.addEventListener('click', toggleShuffle);
305+
previousProblemButtonEl.addEventListener('click', previousProblem);
285306
nextProblemButtonEl.addEventListener('click', nextProblem);
286307

287308
// start it up!

0 commit comments

Comments
 (0)