0

I have created a function in order to tidy up some code but the function is not returning the value that I want. I am trying to remove 1 from the current value of 'lives' which is 20 but when calling the function its returning the value 20.

This is the function that I have created:

const removeLives = function(lives) {
  return lives--;
}

and I'm calling it here:

     // if guess is too high
    } else if (userNumber > randomNumber) {
      document.querySelector('.message').textContent = 'Too high! ☝️';
      removeLives(lives);
      document.querySelector('.lives').textContent = `Lives: ${lives}`;

This is the code I had previously and it works fine:

      // if guess is too high
    } else if (userNumber > randomNumber) {
      document.querySelector('.message').textContent = 'Too high! ☝️';
      lives--;
      document.querySelector('.lives').textContent = `Lives: ${lives}`;

I'm a beginner to this stuff and any help would be appreciated!

cfm200
  • 1
  • 1
  • 1
    `lives` inside of your function is a copy of the `lives` value you passed to `removeLives()` when you called it, so modifying it does not modify the one from that was passed into the function (that only applies for object references). In your case, you would could use `lives = removeLives(lives);` as an option as the value you return from `removeLives()` will be reassinged back into the `lives` variable (but in that case, you're better just doing `return lives-1` or `return --lives`). – Nick Parsons Jul 22 '23 at 13:54
  • 1
    @CodemasterUnited No, it will not. Have you tried doing so? – InSync Jul 22 '23 at 13:55

0 Answers0