Mastermind Tests Not working

javascript
#1

Example from Brandon

if i put in the correct solution the genereate hint still gives the wrong answere

here is my master mind function

function mastermind(guess) {
  //solution = 'abcd'; // Comment this out to generate a random solution
  if (isValid(guess)) {
    console.log(guess, " Guess");
    console.log(solution, " Solution");
    let win = "You guessed it";
    let outOfTurns = "You ran out of turns! The solution was " + solution;
    let guessAgain = "guess again";
    if (guess === solution) {
      let hint = generateHint(solution, guess);
      let guessAndHint = guess + " " + hint;
      board.push(guessAndHint);
      console.log(win, " win");
    } else {
      let hint = generateHint(solution, guess);
      let guessAndHint = guess + "-" + hint;
      board.push(guessAndHint);
      console.log(hint, " Hint");
    }
    if (board.length === 10) {
      console.log(outOfTurns, " Out of Turns");
      return outOfTurns;
    } else {
      console.log(guessAgain);
      return guessAgain;
    }
  } else {
    console.log(colors.red("please enter a valid guess"));
  }
}

here is my generate hint function

function generateHint(guess, solution) {
  let correctLetterLocations = 0;
  let correctLetters = 0;
  let solutionArray = solution.split("");
  let guessArray = guess.split("");

  for (let i = 0; i < solutionArray.length; i++) {
    let letter = solutionArray[i];
    let guess = guessArray[i];
    if (letter === guess) {
      correctLetterLocations++;
      solutionArray[i] = null;
      console.log("letters");

    }

  }
  for (let j = 0; j < solutionArray.length; j++) {
    let letter = solutionArray[j];
    let targetIndex = guessArray.indexOf(letter);
    if (targetIndex > -1) {
      correctLetters++;
      solutionArray[j] = null;
    }
  }
  return (colors.red(correctLetterLocations) + "-" + colors.white(correctLetters));
}

Thanks for the help!

1 Like
#2

I’ve seen the tests fail when adding the colors directly to the generateHint() return statement, as the test just looks for the format 2-1. You’ll probably need to create a helper like

function generateHint(guess, solution) {
  //...
  return (colors.red(correctLetterLocations) + "-" + colors.white(correctLetters));
}

const colorItUp = hint =>  {
  const splitHint = hint.split('-');
  return  `${colors.red(splitHint[0])}-${colors.white(splitHint[1])}`;
};

// colorItUp(generateHint('abcd', 'cdba'));
1 Like
#3

I gave the students both options.

Update the test to look for the colorized strings or log the colorized and return the un edited.

1 Like