Skip to content

Instantly share code, notes, and snippets.

@I-keep-trying
Created December 10, 2020 22:02
Show Gist options
  • Save I-keep-trying/7e9111c620dc968ba8f122f0852255fe to your computer and use it in GitHub Desktop.
Save I-keep-trying/7e9111c620dc968ba8f122f0852255fe to your computer and use it in GitHub Desktop.
String replace/filter/whatever
import { object } from 'prop-types'
import React, { useState } from 'react'
const stringArray = [
{ word: 'bassoon', score: 371 },
{ word: 'barroon', score: 371 },
{ word: 'babboon', score: 371 },
]
const App = () => {
const [correctLetters, setCorrectLetters] = useState([])
const [wrongLetters, setWrongLetters] = useState([])
const [results, setResults] = useState([])
const [placeholders, setPlaceholders] = useState([])
let input = 'ba1122n'
/* React.useEffect(() => {
setResults(results)
}, [results]) */
/* const getPlaceholders = () => {
for (let i = 0; i < 10; i++) {
const regexes = new RegExp(`(${i})`, 'g')
let p = [...input.matchAll(regexes)]
if (p.length > 0) {
setPlaceholders((state) => [...state, p])
}
}
}
React.useEffect(() => {
getPlaceholders()
}, []) */
const compareWords = () => {
let status = ''
const inputArr = input.split('')
stringArray.map((obj) => {
obj.word.split('').forEach((targetLetter, i) => {
inputArr.map((inputLetter, index) => {
if (!obj.word.includes(inputLetter) && i === index) {
console.log('input index', index)
console.log('')
/* console.log(
'inputLetter',
inputLetter, //inputLetter 1, 1, 2, 2,
'targetLetter',
targetLetter //targetLetter r, r, o, o
) */
//const regexes = new RegExp(`(${j})`, 'g')
let p = [...input.matchAll(inputLetter)]
console.log('p', p) // separate sets of arrays, one set for each placeholder number
// p.forEach((item, k) => {
// const inputChar = item[0]
// const indexJ = item.index
// const targetChar = obj.word.charAt(indexJ)
/* console.log(
'indexJ',
indexJ, // 2, 3, and 4, 5
'input char (p.item[0]) ',
inputLetter, // 1, 1, and 2, 2
'targetChar',
targetLetter // r, r, o, o
) */
const countInput = [...input].reduce((a, e) => {
if (e === inputLetter) {
a[e] = a[e] ? a[e] + 1 : 1
}
return a
}, {})
const countWord = [...obj.word].reduce((a, e) => {
if (e === targetLetter) {
a[e] = a[e] ? a[e] + 1 : 1
}
return a
}, {})
if (Object.values(countInput)[0] !== Object.values(countWord)[0]) {
//self destruct
/* console.log(
'Object.values(countInput)[0] !== Object.values(countWord)[0]',
obj.word // babboon
) */
setResults(
stringArray.filter((result) => {
// console.log('setResults filter', result) // WHY WHY WHY WHY why is this happening when the condition is not met??????????????????
return result === obj.word ? null : result // and ALSO not doing this!! Anyway!
})
)
}
const newWord = [...input].reduce((a, e) => {
if (e === inputLetter) {
/* console.log('input reducer', [...input])
console.log('input reducer index', index)
console.log('reducer a', a)
console.log('reducer e', e)
console.log(
'inputLetter',
inputLetter,
'target letter',
targetLetter,
'obj.word',
obj.word
) */
}
return a
}, {})
const testWord = input.replaceAll(inputLetter, targetLetter)
console.log('testWord', testWord)
if (input === obj.word) {
console.log('testWord === obj.word')
// setResults((results) => [...results, input])
}
// })
/* for (let j = 0; j < p.length; j++) {
const inputChar = p[j][0]
const indexJ = p[j].index
const targetChar = obj.word.charAt(indexJ)
console.log(
'indexJ',
indexJ,
'input char ',
inputChar,
'targetChar',
targetChar
)
if (targetChar !== inputChar) {
let testWord = input.replaceAll(inputChar, targetChar)
console.log('testWord', testWord)
if (testWord === obj.word) {
setResults((results) => [...results, testWord])
}
}
} */
console.log('---------------------')
}
return inputLetter
})
})
return obj.word
})
console.log('newResults', results)
return status
}
const handleInput = () => {
const inputArr = input.split('')
inputArr.map((letter, i) => {
console.log('input letter', letter)
console.log('input word index', i)
return letter
})
}
console.log('results', results)
return (
<div>
<button onClick={handleInput}>start</button>
<button onClick={compareWords}>compare</button>
{JSON.stringify(results)}
</div>
)
}
export default App
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment