0

I'm trying to make a counter, that can only go from 0, to a max of 10, right now I can add and delete number from the counter, but for some reason my if/else never works, no matter where I put them. I'm a real noob to javascript so if anyone could give me some advice it would be highly appreciated.

Here's my code:

 <script type="text/javascript">
var clicks = 0;
if (clicks < 0) {
    clicks = 0;
    document.getElementById("ticketAmmount").innerHTML = clicks;
}
function onClick() {
    clicks += 1;
    document.getElementById("ticketAmmount").innerHTML = clicks;
};
function offClick() {
    clicks -= 1;
    document.getElementById("ticketAmmount").innerHTML = clicks;
}

i also have tried this: but then the counter gives me NaN

<script type="text/javascript">
var clicks = 0;

function onClick() {
if (clicks < 0) {
    clicks = 0;
    document.getElementById("ticketAmmount").innerHTML = clicks;
} else {
 clicks += 1;
    document.getElementById("ticketAmmount").innerHTML = clicks;
}

};
function offClick() {
    clicks -= 1;
    document.getElementById("ticketAmmount").innerHTML = clicks;
}
</script>
7
  • 2
    Why do you check if clicks < 0 right after setting it to 0? Commented Mar 4, 2015 at 13:33
  • 2
    where is the if/else? Commented Mar 4, 2015 at 13:34
  • If the if statement you're showing in the code block is any indication of where you're trying to put your if/else blocks, they'll only ever run once. You need to put them in your functions if you want them to run more than once. Commented Mar 4, 2015 at 13:35
  • i thought that everytime you hit the button, the script reads it from bottom to top, so if the clicks is smaller then 0, it should reset the counter back to 0 instead of going to -1 Commented Mar 4, 2015 at 13:35
  • 2
    So add the if check inside of the in/off clicks Commented Mar 4, 2015 at 13:35

3 Answers 3

2

You were almost right, put your if within your onClick and offClick functions, and remove the first one (that doesn't seem useful):

var clicks = 0;

document.getElementById("ticketAmmount").innerHTML = clicks;

function onClick() {
    clicks += 1;
    if (clicks > 10) {
        clicks = 10;
    }
    document.getElementById("ticketAmmount").innerHTML = clicks;
};
function offClick() {
    clicks -= 1;
    if (clicks < 0) {
        clicks = 0;
    }
    document.getElementById("ticketAmmount").innerHTML = clicks;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes it works thanks! It's actually really logic when you think about it!
2

It's not very complicated:

var minVal = 0, maxVal = 10, clicks = 0,
    display = document.getElementById("ticketAmmount");

function countUp(){
  clicks = Math.min( maxVal, clicks+1 );
  display.innerHTML = clicks;
}

function countDown(){
  clicks = Math.max( minVal, clicks-1 );
  display.innerHTML = clicks;
}
<button onclick="countDown();">-</button>
<span id="ticketAmmount">0</span>
<button onclick="countUp();">+</button>

Comments

1

if (clicks < 0) is being called only once, when the page loads, and right after you set clicks = 0, so it never meets the criteria. You need to put that logic in both the onClick and offClick or extract it out in to some method, for example:

var clicks = 0;
function resetClicks() {
    if (clicks < 0) {
      clicks = 0;
      document.getElementById("ticketAmmount").innerHTML = clicks;
    }
}

function onClick() {
    clicks += 1;
    document.getElementById("ticketAmmount").innerHTML = clicks;
    resetClicks();
};
function offClick() {
    clicks -= 1;
    document.getElementById("ticketAmmount").innerHTML = clicks;
    resetClicks();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.