Decrement until 0

2,303 views
Skip to first unread message

Lowell Klassen

unread,
Jan 13, 2015, 10:33:05 PM1/13/15
to androi...@googlegroups.com
Hi, can someone tell me how to decrement in the android app builder?

I have been using:

var count = 0;

//for incrementing
count++;

//for decrementing
count--;

--------------------

The numbers go into the negative though. I want it to stop at 0. I only want to be able to go higher than 0. Please help. Thanks.

Steve Garman

unread,
Jan 14, 2015, 4:14:29 AM1/14/15
to androi...@googlegroups.com
To stop decrementing below 0, you can use
if (count > 0 ) count-- ;
but I suspect you may be looking for some kind of loop.

You might like to look at http://www.w3schools.com/js/js_loop_for.asp

Chris Hopkin

unread,
Jan 14, 2015, 4:14:42 AM1/14/15
to androi...@googlegroups.com
Hi Lowell

You will have to check if count is 0 before decrementing to prevent it from going negative. The most straightforward way would be:

// Only decrement if count is 1 or more - count will never go negative
if(count > 0)
{
   count
--;
}

Hope that helps? Perhaps if you explain a bit more about what you are trying to do, we might be able to provide more help.

Chris

Chris Hopkin

unread,
Jan 14, 2015, 4:21:24 AM1/14/15
to androi...@googlegroups.com
Sorry Steve, didn't see your answer when I posted - looks like we both posted the same answer at the same time! :)
Message has been deleted

Lowell Klassen

unread,
Jan 14, 2015, 10:45:37 AM1/14/15
to androi...@googlegroups.com
Thank-you very much gentlemen! Bright minds think alike! :D I will probably be posting on here a lot, as I'm a newbie to javascript.

Lowell Klassen

unread,
Jan 14, 2015, 12:31:25 PM1/14/15
to androi...@googlegroups.com
Sorry to bother you again gentlemen. I'm trying to put it into this function with no success in the android app:

function countDown () {
}

luke x3

unread,
Jan 31, 2015, 3:46:33 AM1/31/15
to androi...@googlegroups.com
something like this?

function countDown(thisVar){
 
return (thisVar - 1) * (thisVar > 0);
 
}

// using countDown()
var myvar = 2;

myvar
= countDown(myvar);


Reply all
Reply to author
Forward
0 new messages