when we press '-' button it is increasing 1 time and decreasing why this is happening? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 3

when we press '-' button it is increasing 1 time and decreasing why this is happening?

https://code.sololearn.com/WxBMUA0ghUY4/?ref=app

2nd Sep 2020, 8:55 AM
Mr. 12
Mr. 12 - avatar
16 Respostas
+ 3
Because value = 1
2nd Sep 2020, 9:00 AM
Muhammadamin
Muhammadamin - avatar
+ 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
2nd Sep 2020, 9:31 AM
Oma Falk
Oma Falk - avatar
+ 2
Please use x=++value and x=--value. It will work like a charm
2nd Sep 2020, 9:00 AM
Oma Falk
Oma Falk - avatar
+ 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?
2nd Sep 2020, 9:05 AM
Mr. 12
Mr. 12 - avatar
+ 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
2nd Sep 2020, 9:07 AM
Muhammadamin
Muhammadamin - avatar
+ 1
yes Muhammadamin i tried, i want to know the reason why it is adding when i pressed - button
2nd Sep 2020, 9:08 AM
Mr. 12
Mr. 12 - avatar
+ 1
It is not adding when you clicked first, it is only shows the the text of value, and here value equals 1
2nd Sep 2020, 9:10 AM
Muhammadamin
Muhammadamin - avatar
+ 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 šŸ„¶šŸ„¶šŸ„¶šŸ„¶šŸ„¶šŸ„¶šŸ„¶šŸ„¶
2nd Sep 2020, 9:12 AM
Oma Falk
Oma Falk - avatar
+ 1
i didn't understand Oma Falk
2nd Sep 2020, 9:14 AM
Mr. 12
Mr. 12 - avatar
+ 1
Wait..
2nd Sep 2020, 9:18 AM
Oma Falk
Oma Falk - avatar
+ 1
Mr. 12 val = 3 X= ++val ---> x=4 and val =4 okay?
2nd Sep 2020, 9:22 AM
Oma Falk
Oma Falk - avatar
+ 1
i know that bro, Oma Falk but value = 3 value-- means 2 right, i didn't understand that
2nd Sep 2020, 9:24 AM
Mr. 12
Mr. 12 - avatar
+ 1
Mr. 12 now x= val++ --> x=4 and val =5 You see the difference between val++ and ++val?
2nd Sep 2020, 9:25 AM
Oma Falk
Oma Falk - avatar
+ 1
now what happened to you X=val -- -->x = 5 and val=4
2nd Sep 2020, 9:27 AM
Oma Falk
Oma Falk - avatar
+ 1
yes Oma Falk i know, i just used this 'value++; gu.innerHTML=value;' and it is working fine. i just overthinked...
2nd Sep 2020, 9:28 AM
Mr. 12
Mr. 12 - avatar
+ 1
thank you Oma Falk and everyone
2nd Sep 2020, 9:29 AM
Mr. 12
Mr. 12 - avatar