Currently I am building a Hanging man game, where I want to store the old array length
and compare it to the new array length
. I know that useRef
is the stuff I need to get this done. Could someone help me with this.
useEffect(() => {
const checkLetter = (event) => {
let letter = String.fromCharCode(event.keyCode).toLowerCase();
if(event.keyCode >= 65 && event.keyCode <= 90) {
setCount(count + 1);
setGuessed(prev => {
const next = [...prev, letter]
counter(next);
return next;
});
}
}
document.addEventListener('keydown', checkLetter);
return () => {
document.removeEventListener('keydown', checkLetter);
}
}, [guessed, count]);
const counter = (letterArray) => {
let oldArray = letterArray.filter((v, i) => letterArray.indexOf(v) === i);
// currently oldArray outputs for instance ['a', 'b', 'c'];
// if someone clicks on a new letter for instance 'd', the new updated array becomes ['a', 'b', 'c', 'd']. And if I want to compare the old array with new updated array for instance like: oldArray !== newUpdatedArray, it returns true.
}
if current old array is ['a', 'b', 'c']
and you recently clicked on letter d, the new updated array becomes ['a', 'b', 'c', 'd']
. And then i want to compare ['a', 'b', 'c'] !== ['a', 'b', 'c', 'd']
;