1

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;
Amauri
  • 540
  • 3
  • 15

3 Answers3

1

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?

Nick Grealy
  • 24,216
  • 9
  • 104
  • 119
0

You should use --_01

_01-- will do -1 after the expression.

Community
  • 1
  • 1
Chen-Tsu Lin
  • 22,876
  • 16
  • 53
  • 63
  • 1
    You are assuming User3280654 *wants* to change the value of variable `_01`. If `value=_01-1` "works now", that probably isn't true... – Kobi Feb 13 '14 at 06:14
0

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

Ruchira Shree
  • 163
  • 10