Created
September 14, 2022 14:10
-
-
Save MoodyBones/ab73af6f5d5f725be1c65108f15e859d to your computer and use it in GitHub Desktop.
Revisions
-
MoodyBones created this gist
Sep 14, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ // generate hint / score the guess // 0 wrong, 1 right letter wrong place, 2 right letter right place function generateHint(word: string, guess: string): string { // spread word into array const source = [...word] // map guess return [...guess].map((letter, i) => { // if correct letter remove from source & return true if (letter === source[i]) { source[i] = null return true } // otherwise return false return false // map the array again (which is now an array of booleans) }).map((exact, i) => { // console.log(exact) // true or false // if true it means its a correct guess so return '2' if (exact) return '2' // if the letter is in the source return '1' if (source.includes(guess[i])) return '1' // otherwise return '0' return '0' }).join('') } console.log(generateHint('apple', 'label')) // learnt from https://www.youtube.com/watch?v=-U8NWGTGNVw Daniel Roe N3rdle