I'm using indexOf() to get the index of an element inside an array.
fs.readFile(filename, { encoding: 'utf-8' }, (error, data) => {
if( error ) return new Error(error);
let selectedWord;
let charMap = [];
let splittedWord;
let output;
const d = data.split('\n');
for(let i = d.length - 1; i > 0; i--){
const r = Math.floor(Math.random() * (i + 1));
const tmp = d[i];
d[i] = d[r];
d[r] = tmp;
selectedWord = d[r];
}
splittedWord = selectedWord.split('');
splittedWord.forEach( (char, i) => {
charMap.push(char);
});
//console.log(charMap);
console.log(`Selected word length is ${splittedWord.length}`);
let dropped = 0;
let mid = Math.floor(splittedWord.length / 2);
console.log(`Removed letters: ${mid}`);
for (let e = 0; e < splittedWord.length; e++) {
// probability we should drop this one is
// (desiredDropped - dropped) / (word.length - e)
if( Math.random() < (mid - dropped) / (splittedWord.length - e) ){
// drop this letter
splittedWord.splice(e, 1, '_');
dropped++;
}
}
//console.log(`Original word: ${selectedWord}`);
console.log(`Guess the word: ${splittedWord}`);
let removedChars = charMap.filter( (char, index) => {
if( splittedWord[index] !== char ){
return char;
}
});
console.log(removedChars);
let charIndex = -1;
rl.prompt();
rl.on('line', (input) => {
//console.log(input);
if( removedChars.includes(input) && charMap.includes(input) ){
charIndex = charMap.indexOf(input, charIndex + 1);
//console.log(charIndex);
splittedWord.splice(charIndex, 1, input);
// splittedWord = splittedWord.map( (char, i) => {
// charMap[i] === input ? input : char
// });
console.log(`Well done!Gues the next char: ${splittedWord}`);
rl.prompt();
} else if( splittedWord.join() === selectedWord ){
console.log('Congratulation, you have found word!');
rl.close();
process.exit();
} else {
console.log(`Oh noo, wrong letter! Try with a different one!`);
rl.prompt();
}
});
});
I've noticed that if in the searched array there are two identical elements at a different index, only one will be found and the second will be ignored. Is there a solution for this?
Consider this concole output, I have the word grifferesti, since the letter i is present two times, indexOf will only give me the first element found that match the searching criteria. This become a problem because a word can contain duplied charatchers and I need to check for them if they are removed from the word that the user will display and try to guess
[
'g', 'r', 'i', 'f',
'f', 'e', 'r', 'e',
's', 't', 'i'
]
Selected word length is 11
Removed letters: 5
Original word: grifferesti
Guess the word: g,r,_,_,f,e,r,e,_,_,_
[ 'i', 'f', 's', 't', 'i' ]
Please insert a letter: i
i
2
Well done!Gues the next char: g,r,i,_,f,e,r,e,_,_,_
Please insert a letter: f
f
3
Well done!Gues the next char: g,r,i,f,f,e,r,e,_,_,_
Please insert a letter: t
t
9
Well done!Gues the next char: g,r,i,f,f,e,r,e,_,t,_
Please insert a letter: i
i
2
Well done!Gues the next char: g,r,i,f,f,e,r,e,_,t,_
Please insert a letter: s
s
8
Well done!Gues the next char: g,r,i,f,f,e,r,e,s,t,_
UPDATE
I'm testing the solution provided from the user Hacktish but this is what happen in console when I test the code
Selected word length is 9
Your score: 0
Guess the word: _,_,_,_,_,_,_,_,_
Please insert a letter: a
You found the a letter!
Score: 50
,,,,,,,,
Please insert a letter: c
Oh noo!Wrong letter, please retry!
Please insert a letter: b
Oh noo!Wrong letter, please retry!
Please insert a letter: n
You found the n letter!
Score: 0
,,,,,,,,
As you can see, the _ char will be not displayed and the found letter will be not added. I don't know if this can be an implementation issue
splittedWord = charMap.map( () => '_' );
rl.prompt();
rl.on('line', (input) => {
if( charMap.includes(input) ){
splittedWord = splittedWord.map( (char,i) => {
charMap[i] === input ? input : char;
});
console.log(`You found the ${input} letter!`);
score += 50;
console.log(`Score: ${score}`);
console.log(`${splittedWord}`);
rl.prompt();
} else if( splittedWord.join('') === selectedWord ){
console.log('Yeah! You found the word!');
rl.close();
} else {
console.log('Oh noo!Wrong letter, please retry!');
score -= 50;
rl.prompt();
}
});
filterinstead ofindexOf+splicesplittedWordarray using splice