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
52 changes: 48 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,50 @@ const getRandomInt = (min, max) => {
return Math.floor(Math.random() * (max - min)) + min;
}

const generateHint = () => {
// your code here
const generateHint = (guess) => {

let guessArray = guess.split('');
let solutionArray = solution.split('');
let fullHints = 0;
let halfHints = 0;

for(let i = 0; i < guessArray.length; i++){
if(guessArray[i] === solutionArray[i]){
fullHints++;
solutionArray[i] = 0;
}else if(solutionArray.indexOf(guessArray[i]) > -1){
halfHints++;
solutionArray[solutionArray.indexOf(guessArray[i])] = 1;
}
}

return fullHints + '-' + halfHints;
}

const 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

if(typeof guess !== 'string' || guess.length !== 4){
console.log('Guess must be 4 letters long using only letters A-H');
return false;
}

guess = guess.toLowerCase().trim();

if(guess == solution){
board = [];
console.log("You guessed it!");
return "You guessed it!";
}else if(board.length === 10){
console.log("You lost! The solution was " + solution);
board = [];
solution = '';
generateSolution();
}else{
board.push(guess)
console.log(generateHint(guess))
}
}


Expand Down Expand Up @@ -74,4 +111,11 @@ if (typeof describe === 'function') {

generateSolution();
getPrompt();
}
}
//guess must only be letters/strings from letters array and 4 letters in length, if not then log 'can only use 4 letters from range a-h
//if user inputs whitespace and uppercase letters then use trim method tolowercasw method
//generate a solution for the user to guess
//push each guess into the board array
//compare the board array(guess) to the solution array
//iterate over board array(guess) and compare the element at each index to that of the solution array
//if