0
Hello.
I would like that each time I call the function is incremntado the value of ( i ) and displayed in Alert, do not know what I’m doing wrong.
My code;
function incrementar()
{
var count = 0.
count++;
alert(count);
}
Thank you
0
Hello.
I would like that each time I call the function is incremntado the value of ( i ) and displayed in Alert, do not know what I’m doing wrong.
My code;
function incrementar()
{
var count = 0.
count++;
alert(count);
}
Thank you
2
You are resetting the counter every time you call the function, because it is opening var count
inside. Define count
before, as in the example, so that the same variable is available whenever it is called.
var count = 0;
function incrementar() {
count++;
// use console.log ao invés de alert para ver os resultados
// no console do navegador, aperte F12 no Chrome para ver o console.
console.log(count);
}
incrementar();
incrementar();
incrementar();
incrementar();
0
The function will always display the value 1, because the algorithm is setting the value of the variable Count to zero every time the function is executed.
You can solve the problem as follows:
var count = 0;
function incrementar() {
alert(++count);
}
Thanks friend.
Browser other questions tagged javascript
You are not signed in. Login or sign up in order to post.
Nice, but I need the incremented value to be global so I can use in other javascript functions. I thought of setting so outside the function ( var totalIncremento; ) dai inside the function do so ( totalIncremento = Count++; ). It would work this way
– abduzeedo
In this case the incremented value is already global, within the scope you have passed. If you need a value that is really global, to be accessed by other scopes, other scripts, you can use
window.count = 0
before andwindow.count++
within the function.– Ricardo Moraleida