mirror of
https://github.com/genemecija/learn-morse-code.git
synced 2026-04-21 06:03:56 +00:00
Added wordlist randomization in Challenge mode
This commit is contained in:
parent
f6384b30ef
commit
da142ef58a
9 changed files with 80 additions and 30 deletions
|
|
@ -3,19 +3,23 @@ import { WordListPickerContext } from "./wordListPickerContext"
|
|||
const WordFeederContext = React.createContext()
|
||||
|
||||
function WordFeederContextProvider(props) {
|
||||
|
||||
console.log('LOADED: wordFeederContext');
|
||||
// let wordList = ['hi', 'morse', 'code', 'hello', 'gene']
|
||||
const {wordList} = useContext(WordListPickerContext)
|
||||
const {wordList, wordListShuffled} = useContext(WordListPickerContext)
|
||||
|
||||
const [wordIndex, setWordIndex] = useState(0)
|
||||
const [order, setOrder] = useState('sequential')
|
||||
let indexCache = []
|
||||
let word
|
||||
|
||||
let word = wordList[wordIndex]
|
||||
if (order === 'sequential') {
|
||||
word = wordList[wordIndex]
|
||||
}
|
||||
else if (order === 'random') {
|
||||
word = wordListShuffled[wordIndex]
|
||||
}
|
||||
|
||||
function resetFeeder() {
|
||||
setWordIndex(0)
|
||||
indexCache = []
|
||||
}
|
||||
|
||||
function getNextWord() {
|
||||
|
|
@ -23,21 +27,13 @@ function WordFeederContextProvider(props) {
|
|||
setWordIndex(prev => prev + 1)
|
||||
word = wordList[wordIndex] || null
|
||||
} else if (order === 'random') {
|
||||
let index = Math.floor(Math.random*wordList.length)
|
||||
indexCache.push(index)
|
||||
// Need to account for what to do when all words in wordList have been exhausted
|
||||
while (indexCache.includes(index) && wordList.length !== indexCache.length) {
|
||||
index = Math.floor(Math.random*wordList.length)
|
||||
indexCache.push(index)
|
||||
}
|
||||
setWordIndex(index)
|
||||
|
||||
word = wordList[wordIndex]
|
||||
setWordIndex(prev => prev + 1)
|
||||
word = wordListShuffled[wordIndex] || null
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<WordFeederContext.Provider value={{word: word, getNextWord: getNextWord, resetFeeder: resetFeeder}}>
|
||||
<WordFeederContext.Provider value={{word: word, getNextWord: getNextWord, resetFeeder: resetFeeder, setOrder: setOrder}}>
|
||||
{props.children}
|
||||
</WordFeederContext.Provider>
|
||||
)
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ function WordListPickerContextProvider(props) {
|
|||
const [wordListCategory, setWordListCategory] = useState('alphabet')
|
||||
let wordList = []
|
||||
const testList = ['gene', 'anya', 'ali', 'liam', 'last']
|
||||
wordList = testList
|
||||
|
||||
if (wordListCategory === 'alphabet') {
|
||||
wordList = alphabet.words
|
||||
|
|
@ -20,8 +19,30 @@ function WordListPickerContextProvider(props) {
|
|||
wordList = testList
|
||||
}
|
||||
|
||||
|
||||
function randomize(arr) {
|
||||
let array = [...arr]
|
||||
let currentIndex = array.length, temporaryValue, randomIndex;
|
||||
|
||||
// While there remain elements to shuffle...
|
||||
while (0 !== currentIndex) {
|
||||
|
||||
// Pick a remaining element...
|
||||
randomIndex = Math.floor(Math.random() * currentIndex);
|
||||
currentIndex -= 1;
|
||||
|
||||
// And swap it with the current element.
|
||||
temporaryValue = array[currentIndex];
|
||||
array[currentIndex] = array[randomIndex];
|
||||
array[randomIndex] = temporaryValue;
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
|
||||
return (
|
||||
<WordListPickerContext.Provider value={{wordList: wordList, setWordListCategory: setWordListCategory}}>
|
||||
<WordListPickerContext.Provider value={{wordList: wordList, wordListShuffled: randomize(wordList), setWordListCategory: setWordListCategory}}>
|
||||
{props.children}
|
||||
</WordListPickerContext.Provider>
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue