Would this JavaScript code work?

I am wondering if this code I wrote will work with a number guessing game like the one in the online Web Development course. This is what I came up with but it did not seem to fit in with the other JavaScript code that links up with what they were asking the learner to produce for the game to work properly.

let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;

// Write your code below:

const generateTarget = () => {
    return Math.floor(Math.random() * 10)
};

const compareGuesses = (userGuess, computerGuess, secretNumber) => {
  secretNumber = generateTarget();
  if (Math.abs(secretNumber - userGuess) < Math.abs(secretNumber - computerGuess)) {
    return true;
  } else if (Math.abs(secretNumber - userGuess) > Math.abs(secretNumber - computerGuess)) {
    return false;
  } else if (Math.abs(secretNumber - userGuess) === Math.abs(secretNumber - computerGuess)) {
    return true;
  }
};


const updatedScore = (string) => {
  if ('human') {
    return humanScore + 1;
  }
  else if ('computer') {
    return computerScore + 1;
  }
};

const advancedRound = () => {
  currentRoundNumber + 1
};



What I wrote is different from the solution and I am just wondering if it did not work because of the other code that was written specifically for the solution they were looking for and if mine could work if I was able to create the rest of the game to fit my code above ^^^

Here is what Codecademy had included to fit the solution they were looking for…

let target;

const humanGuessInput = document.getElementById('human-guess');

const roundNumberDisplay = document.getElementById('round-number');

const computerGuessDisplay = document.getElementById('computer-guess');
const humanScoreDisplay = document.getElementById('human-score');
const computerScoreDisplay = document.getElementById('computer-score');
const targetNumberDisplay = document.getElementById('target-number');
const computerWinsDisplay = document.getElementById('computer-wins');

const guessButton = document.getElementById('guess');
const nextRoundButton = document.getElementById('next-round')

guessButton.addEventListener('click', () => {
  // Generate the target value
  target = generateTarget();
  // Retrieve the player's guess
  const currentHumanGuess = humanGuessInput.value;
  // Make a random 'computer guess'
  const computerGuess = Math.floor(Math.random() * 10);

  // Display the computer guess and the target
  computerGuessDisplay.innerText = computerGuess;
  targetNumberDisplay.innerText = target;
  
  // Determine if the human or computer wins:
  const humanIsWinner = compareGuesses(currentHumanGuess, computerGuess, target)
  const winner = humanIsWinner ? 'human' : 'computer'

  // Update the correct score:
  updateScore(winner);

  // Display the winner
  if (humanIsWinner) {
    guessButton.innerText = 'You Win!!!!!';
    guessButton.classList.toggle('winning-text')
  } else {
    computerWinsDisplay.innerText = 'Computer Wins!!!';
  }

  // winnerDisplay.innerText = humanIsWinner ? 'You win!' : 'Computer wins!';

  // Display the current scores:
  humanScoreDisplay.innerText = humanScore;
  computerScoreDisplay.innerText = computerScore;
  
  // Set the correct disabled state for the buttons
  guessButton.setAttribute('disabled', true)
  nextRoundButton.removeAttribute('disabled');
});

nextRoundButton.addEventListener('click', () => {
  // Increase the round number
  advanceRound();
  // Display the new round number
  roundNumberDisplay.innerText = currentRoundNumber;

  // Set the correct disabled state for the buttons
  nextRoundButton.setAttribute('disabled', true);
  guessButton.removeAttribute('disabled');

  // Reset the guess input box and the target number display:
  targetNumberDisplay.innerText = '?';
  guessButton.innerText = 'Make a Guess';
  humanGuessInput.value = '';
  computerGuessDisplay.innerText = '?';
  computerWinsDisplay.innerText = '';
  guessButton.classList.remove('winning-text');
});

const addButton = document.getElementById('add');
const subtractButton = document.getElementById('subtract');

addButton.addEventListener('click', () => {
  humanGuessInput.value = +humanGuessInput.value + 1;
  handleValueChange(humanGuessInput.value);
});

subtractButton.addEventListener('click', () => {
  humanGuessInput.value = +humanGuessInput.value - 1;
  handleValueChange(humanGuessInput.value);
});

const handleValueChange = value => {
  if (value > 0 && value <= 9) {
    subtractButton.removeAttribute('disabled');
    addButton.removeAttribute('disabled');
  } else if (value > 9) {
    addButton.setAttribute('disabled', true);
  } else if (value <= 0) {
    subtractButton.setAttribute('disabled', true);
  }
}

humanGuessInput.addEventListener('input', function(e) {
  handleValueChange(e.target.value);
});

Thanks

You would have to make significant changes to the game.js for your code to work. Your functions do not update the global varialbes:

let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;

One returns a value, but they don’t assign the incremented values to the variables. Your code would work with the current game.js file if you modified these two functions to update the variables rather than return values.

You might also consider how you could refactor this function to not use either else if.

Hint:

Try combining < and === into a single <= or if you’re up to it, you can simply return the result of a single comparison. For example:

const a = 1;
const b = 2;

const compare = (a, b) => a <= b; //returns true if a is less than or equal to b, or false otherwise

This is great I got it work now and see by your reply I should have remembered the increment operator. Thank you. Also a big problem was the miss spelling of the functions compared to what they were looking for to match up with their game.js code. I had advancedRound, instead of advanceRound and updatedScore, instead of updateScore.

1 Like