Skip to content

counter project - clicking reset, then add/remove bees does not start at 0. #1

Description

@billliu1992

To reproduce:

  1. Click "+1" 3 times.
  2. Click "Reset."
  3. Click "+1" again.

Expected behavior: Counter shows 1.
Actual behavior: Counter shows 4.

Looks like your event handler for "reset" does not update currentNumber to 0.

Personally, I'd separate the state updates and DOM updates. That way, you can reduce some duplication. It would maybe look like this (untested):

function init() {
  let currentNumber = 0;
  plusBee.addEventListener("click", function () {
    currentNumber++;
    updateHtml(currentNumber);
  });

  minusBee.addEventListener("click", function () {
    currentNumber = Math.max(0, currentNumber - 1); // Minimum number of bees is 0.
    updateHtml(currentNumber);
  });

  resetBtn.addEventListener("click", function () {
    currentNumber = 0;
    updateHtml(currentNumber);
  });
}

function updateHtml(beesCount) {
  numberText.textContent = beesCount;
  messageText.textContent = beesCount > 0
    ? "You've seen " + beesCount + " bees! (´∀`)"
    : "";
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions