diff --git a/04week/mastermind.js b/04week/mastermind.js index 60e5cfa18..fdb5b8363 100644 --- a/04week/mastermind.js +++ b/04week/mastermind.js @@ -21,6 +21,7 @@ function generateSolution() { for (let i = 0; i < 4; i++) { const randomIndex = getRandomInt(0, letters.length); solution += letters[randomIndex]; + } } @@ -28,13 +29,53 @@ function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min)) + min; } -function generateHint() { +function generateHint(guess) { // your code here + let solutionArray = solution.split(""); + let guessArray = guess.split(""); + let correctLetterLocation = 0; + let correctLetters = 0; + + for(let i = 0; i < 4; i++){ + if(solutionArray === guessArray){ + correctLetterLocation++; + solutionArray = null; + } + } + console.log(solutionArray); + console.log(guessArray); + + for(let i = 0; i < 4; i++){ + let targetIndex = solutionArray.indexOf(guessArray[i]); + if(targetIndex < -1) { + correctLetters++; + solutionArray[targetIndex] = null; + console.log(solutionArray); + } + } + + return correctLetterLocation + "-"+ correctLetters; + + +} + +function checkForWin(guess, solution){ + if(guess === solution){ + return true; + } else{ + return; + } } function mastermind(guess) { - solution = 'abcd'; // Comment this out to generate a random solution + //solution = 'abcd'; // Comment this out to generate a random solution // your code here + let hint = generateHint(guess) + board.push(`Guess: ${guess} - Hint: ${hint}`); + if(checkForWin(guess, solution) == true){ + return "You Guessed it!"; + } + } diff --git a/JS211_ArrayPractice b/JS211_ArrayPractice new file mode 160000 index 000000000..1e00855c6 --- /dev/null +++ b/JS211_ArrayPractice @@ -0,0 +1 @@ +Subproject commit 1e00855c6576c929c7efa4a62225a720d36dd08a