Question

I was having trouble with a piece of code and found out that the problem was that decrement (--) was not doing anything. Instead I am using -1, but what is it that it doesn't funciton?

_01 is simply a number

 minOne = document.getElementById("ctdwnTimeDispSec").value=_01--;

This is what works now

 minOne = document.getElementById("ctdwnTimeDispSec").value=_01-1;
Was it helpful?

Solution 2

You should use --_01

_01-- will do -1 after the expression.

OTHER TIPS

The -- operator will decrement the number it's operated on, during or after the statement, based on whether it is placed before or after the number.

e.g. Placing -- after a, will modify the value of a, on the following line.

var a=5
var b=a--

afterwards, will equal:

a=4
b=5

e.g. Placing -- before a, will modify a on the same line.

var a=5
var b=--a

afterwards, will equal:

a=4
b=4

When you use var b=a-1, the javascript will execute the a-1 on that line, making b=4, and not changing a. Make sense?

You will need to use the pre decrrement operator instead of post decrement operator, other you can get the decremented value after that expression.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top