Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions 04week/mastermind.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,61 @@ function generateSolution() {
for (let i = 0; i < 4; i++) {
const randomIndex = getRandomInt(0, letters.length);
solution += letters[randomIndex];

}
}

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!";
}

}


Expand Down
1 change: 1 addition & 0 deletions JS211_ArrayPractice
Submodule JS211_ArrayPractice added at 1e0085