learn-morse-code/src/app-modes/ChallengeMode.js

75 lines
2.8 KiB
JavaScript
Raw Normal View History

2020-01-19 04:35:11 +01:00
import React, {useContext} from 'react';
2020-01-09 10:06:38 +01:00
import '../css/App.css';
import morseCode from '../data/morse-reverse.json'
2020-01-19 04:35:11 +01:00
import ChallengeWord from '../components/ChallengeWord'
import ChallengeBufferDisplay from '../components/ChallengeBufferDisplay';
2020-01-19 04:35:11 +01:00
import { MorseBufferContext } from '../contexts/morseBufferContext';
2020-01-09 10:06:38 +01:00
2020-01-21 11:04:46 +01:00
export default React.memo(function ChallengeMode() {
2020-01-09 10:06:38 +01:00
2020-01-21 11:04:46 +01:00
console.log("ChallengeMode loaded");
let wordList = ['morse', 'code', 'hello', 'gene']
let word = wordList.shift()
let challengeLetters = word.split('')
2020-01-19 19:26:55 +01:00
2020-01-21 11:04:46 +01:00
const {morseCharBuffer} = useContext(MorseBufferContext)
let morseArray = morseCharBuffer.split('_').filter(l => l !== '')
2020-01-21 11:04:46 +01:00
let correctCharIndexes = [] // Indexes of correct letters in Challenge Word
let incorrectCharIndex = null
let incorrectMorseIndexes = [] // Indexes of incorrect morse characters in morse character buffer
// Iterate through the morse character buffer and compare with each letter of challenge word
morseArray.forEach((item, index) => {
2020-01-21 11:04:46 +01:00
let offset = incorrectMorseIndexes.length || 0
2020-01-19 04:35:11 +01:00
2020-01-21 11:04:46 +01:00
let morseLetter = morseCode[morseArray[index+offset]]
let challengeLetter = challengeLetters[index-offset]
if (morseLetter === challengeLetter) {
correctCharIndexes.push(index-offset)
incorrectCharIndex = null
}
else {
2020-01-21 11:04:46 +01:00
incorrectCharIndex = index-offset
incorrectMorseIndexes.push(index+offset)
}
2020-01-21 11:04:46 +01:00
// let offset = incorrectMorseIndexes.length
// if (morseArray[index + offset]) { // If value exists at index
// let morseAlpha = morseCode[morseArray[index + offset]]
// let adjustedIndex = index - offset
// if (challengeLetters[adjustedIndex]) { // If value exists at index
// let challengeLetter = challengeLetters[adjustedIndex].toLowerCase()
// if (morseCharBuffer.slice(-1) === "_") { // Signifies buffer has complete characters (i.e. no partial characters)
// if (morseAlpha === challengeLetter) {
// correctCharIndexes.push(adjustedIndex)
// incorrectCharIndex = null
// } else {
// incorrectMorseIndexes.push(index)
// incorrectCharIndex = adjustedIndex
// }
// }
// }
// else { word = wordList.shift() }
// }
})
2020-01-09 10:06:38 +01:00
return (
<>
2020-01-21 11:04:46 +01:00
<ChallengeWord word={word} correctCharIndexes={correctCharIndexes} incorrectCharIndex={incorrectCharIndex} />
<ChallengeBufferDisplay morseArray={morseArray} incorrectMorseIndexes={incorrectMorseIndexes} />
{/* <button onClick={() => console.log(morseCharBuffer)}>morseCharBuffer</button> */}
2020-01-09 10:06:38 +01:00
</>
);
2020-01-21 11:04:46 +01:00
});