diff --git a/main.js b/main.js index 64ca54b..c29bcd9 100644 --- a/main.js +++ b/main.js @@ -28,13 +28,46 @@ const getRandomInt = (min, max) => { return Math.floor(Math.random() * (max - min)) + min; } -const generateHint = () => { +const generateHint = (guess) => { // your code here + let solutionArray = []; + let guessArray = []; + let correctLetterLocations = 0; + let correctLetters = 0; + + solutionArray = solution.split(''); + guessArray = guess.split(''); + +for(let i = 0; i < solutionArray.length; i++){ + if(solutionArray[i] == guessArray[i]){ + correctLetterLocations ++; + solutionArray[i] = null; +} +} +for(let i = 0; i < solutionArray.length; i++){ + let targetIndex = solutionArray.indexOf(guessArray[i]); + if(targetIndex > -1){ + correctLetters ++; + solutionArray[targetIndex] = null; +} } +return `${correctLetterLocations}-${correctLetters}`; +} + const mastermind = (guess) => { - solution = 'abcd'; // Comment this out to generate a random solution // your code here + if(guess === solution) { + return "You guessed it!"; +} +let hint = generateHint(guess); +board.push(`${guess}, ${hint}`); +if(board.length > 9) { + return `Ran out of guesses. The solution was ${solution}!`; +} + else { + "Good try." +} }