Skip to content
Open
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
37 changes: 35 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}


Expand Down