General question about using functions

When you click the button it changes the value.

All that matters is when you access the value. It obviously has to happen after the button click, otherwise, you will see the value from before the button click.

<button id="buttonA">+</button>
<button id="buttonB">-</button>

<div id="output"></div>

$(document).ready(function () {
  let x = 21;

  function render() {
    $("#output").text(x);
  }

  render();

  $("#buttonA").click(function () {
    x++;
    render();
  });

  $("#buttonB").click(function () {
    x--;
    render();
  });
});

ok, so this is the project I am working on
https://codepen.io/ranran212/pen/yLMOeag?editors=1011

I start with the variable sessionLen = 25 and I can increment and decrement it via two buttons. I can have the display updated with the correct updated value but when I try to use that value in the countdown function sessionLen goes back to 25.
I have tried different options, and really I do not understand, since I call the countdown function AFTER clicking the increment/decrement buttons so the function should use the updated value :frowning:

but I still have the problem of accessing the updated value of sessionLen

You’ve declared secs at the very beginning of the document ready function, which is great. And you are accessing it in the countdown function, which is perfectly fine. But I’m not seeing anywhere else in your code where you are modifying the value of secs. So the first time you call countdown it is always going to equal the original value set at the beginning of the document ready function.

1 Like

I want to thank you for your patience and assistance since I tried to follow your hints and tried to think “like the program” so I ended up completing the project with the functionality I wanted, even if it is not passing all tests (which i do not understand why :frowning: )

https://codepen.io/ranran212/pen/yLMOeag?editors=1011

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.