+ 3
when we press '-' button it is increasing 1 time and decreasing why this is happening?
16 ответов
+ 3
Because value = 1
+ 3
Mr. 12 i know that bro, Oma Falk but
value = 3
value-- means 2 right, i didn't understand that
Ahhh there are two things:
Decrement and assignment
X=value--
Is the same as
X=value
Value-=1
While
x=--value
Is the same as
value -=1
X=value
+ 2
Please use x=++value and x=--value.
It will work like a charm
+ 1
Oma Falk why it is adding even when we pressing -
the function will have to reduce the number right?
value++ and value-- will add and sub in the functions
- is added to the sub function.
so when we click on it, it has to reduce right?
+ 1
var x = document.getElementById('btn1');
x.addEventListener('click',add);
var y = document.getElementById('btn2');
y.addEventListener('click',sub);
var gu = document.getElementById('p');
var value = 0;
function add(){
gu.innerHTML=++value;
}
function sub(){
gu.innerHTML=--value;
}
Try this
+ 1
yes Muhammadamin i tried, i want to know the reason why it is adding when i pressed - button
+ 1
It is not adding when you clicked first, it is only shows the the text of value, and here value equals 1
+ 1
Mr. 12 right....and also not right
Val =1 at the beginning
X= val++
Now val is 2 but x is 1.....First assign and then increment!
X=val++
Now x=2 and val is 3
X=val--
Now x=3 and val is 2
🥶🥶🥶🥶🥶
Here you are unlucky because u expected to see x=1
🥶🥶🥶🥶🥶🥶🥶🥶
+ 1
i didn't understand Oma Falk
+ 1
Wait..
+ 1
Mr. 12
val = 3
X= ++val ---> x=4 and val =4 okay?
+ 1
i know that bro, Oma Falk but
value = 3
value-- means 2 right, i didn't understand that
+ 1
Mr. 12
now
x= val++ --> x=4 and val =5
You see the difference between val++ and ++val?
+ 1
now what happened to you
X=val --
-->x = 5 and val=4
+ 1
yes Oma Falk i know, i just used this 'value++;
gu.innerHTML=value;' and it is working fine.
i just overthinked...
+ 1
thank you Oma Falk and everyone