diff --git a/main.js b/main.js index 64ca54b..c399a1e 100644 --- a/main.js +++ b/main.js @@ -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)) + } } @@ -74,4 +111,11 @@ if (typeof describe === 'function') { generateSolution(); getPrompt(); -} \ No newline at end of file +} +//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 \ No newline at end of file