How to make a counter with 1 increments?

Closed due to the fact that the essence of the issue is incomprehensible by the participants Vladimir Martianov , Grundy , cheops , VenZell , Pavel Mayorov Apr 27 '16 at 6:35 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Increasing what? Increase for what event? Show at least some kind of code. Nothing is clear - dluhhbiu
  • Increment ordinary chtoli? - Shadow33
  • Most likely, yes, but something really quite a weak question ... - MaximK
  • @ MaksimKutovoy, most likely not. An example of the closure to get the counter. - Qwertiy
  • five
    @Qwertiy gave us a short question and we guess) - Shadow33

3 answers 3

Like this:

var i = 2; i++; // более короткая запись для i = i + 1. alert(i); // 3 



The first link to google

     var a = 1, b = 1, c, d; c = ++a; alert(c); // 2 d = b++; alert(d); // 1 c = (2+ ++a); alert(c); // 5 d = (2+ b++); alert(d); // 4 alert(a); // 3 alert(b); // 3 
       function createCounter(n) { n = n || 0; return function () { return n++; }; } var c1 = createCounter(), c2 = createCounter(), c3 = createCounter(4); [c3(), c2(), c2(), c1(), c3(), c2()] == "4,0,1,0,5,2"; 
      • n = n == null ? 0 : n; - Is it so accepted? I often met a little more: n = n || 0 n = n || 0 Even if instead of 0 something else is required, it may be better not to compare with null, but to check typeof? - BOPOH
      • @BOPOH, I agree, too smart. n = n || 0 n = n || 0 here fits. typeof is for potentially undeclared global variables. And in general, you can check everything you want :) - Qwertiy