From 2d1151ccb14ed7a47097233224ff27eedbac214e Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 22 Apr 2023 21:39:20 -0500 Subject: [PATCH 1/3] code written, only passing one test --- main.js | 47 +++++++++++++++++++++++++++++++++++++++++++---- package.json | 1 + 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/main.js b/main.js index 64ca54b..5d1104f 100644 --- a/main.js +++ b/main.js @@ -11,6 +11,8 @@ let board = []; let solution = ''; let letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; + + const printBoard = () => { for (let i = 0; i < board.length; i++) { console.log(board[i]); @@ -28,13 +30,50 @@ const getRandomInt = (min, max) => { return Math.floor(Math.random() * (max - min)) + min; } -const generateHint = () => { - // your code here +const generateHint = (guess, solution) => { + solutionArray = solution.split("") + guessArray = guess.split("") + correctLetterLocations = 0 + correctLetters = 0 + + + + for (let i = 0; i < solutionArray.length; i++){ + if (solutionArray[i] == guessArray[i]){ + correctLetterLocations++ + solutionArray[i] = null + } + } + + for (let j = 0; j < solutionArray.length; j++){ + targetIndex = solutionArray.indexOf(guessArray[j]) + if (targetIndex > -1){ + correctLetters ++ + solutionArray[targetIndex] = null + } + } + + console.log(correctLetterLocations + '-' + correctLetters) + + } const mastermind = (guess) => { - solution = 'abcd'; // Comment this out to generate a random solution - // your code here + board.push(guess) + if (guess == solution){ + console.log('You guessed it!') + return('You guessed it!') + } else { + generateHint(guess, solution) + } + + if (board.length == 10){ + console.log('You ran out of turns! The solution was ' + solution) + return 'You ran out of turns! The solution was ' + solution + } else { + return "Guess again" + } + } diff --git a/package.json b/package.json index 3afb478..43ea563 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ }, "homepage": "https://github.com/AustinCodingAcademy/JS211_PigLatinProject#readme", "dependencies": { + "colors": "^1.4.0", "eslint": "^3.19.0", "functional-javascript-workshop": "^1.0.6", "htmllint-cli": "github:kevincolten/htmllint-cli", From edd874270c61eb6a96ddddd4a6117269803a4bc6 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 23 Apr 2023 14:34:28 -0500 Subject: [PATCH 2/3] trying to solve trow error for split() --- main.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.js b/main.js index 5d1104f..b68fe0e 100644 --- a/main.js +++ b/main.js @@ -31,8 +31,8 @@ const getRandomInt = (min, max) => { } const generateHint = (guess, solution) => { - solutionArray = solution.split("") - guessArray = guess.split("") + solutionArray = [solution[0],solution[1],solution[2],solution[3]] + guessArray = [guess[0],guess[1],guess[2],guess[3]] correctLetterLocations = 0 correctLetters = 0 From 5792c58282575049b967cc1887ce818bd1adbae4 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 23 Apr 2023 19:57:52 -0500 Subject: [PATCH 3/3] fixed undefined variables --- main.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/main.js b/main.js index b68fe0e..8526f3b 100644 --- a/main.js +++ b/main.js @@ -31,10 +31,10 @@ const getRandomInt = (min, max) => { } const generateHint = (guess, solution) => { - solutionArray = [solution[0],solution[1],solution[2],solution[3]] - guessArray = [guess[0],guess[1],guess[2],guess[3]] - correctLetterLocations = 0 - correctLetters = 0 + let solutionArray = [solution[0],solution[1],solution[2],solution[3]] + let guessArray = [guess[0],guess[1],guess[2],guess[3]] + let correctLetterLocations = 0 + let correctLetters = 0 @@ -46,7 +46,7 @@ const generateHint = (guess, solution) => { } for (let j = 0; j < solutionArray.length; j++){ - targetIndex = solutionArray.indexOf(guessArray[j]) + let targetIndex = solutionArray.indexOf(guessArray[j]) if (targetIndex > -1){ correctLetters ++ solutionArray[targetIndex] = null