+ 2

How does this code work?

I didn't understand how to make a button to switch text back in forward and someone helped me out but I don't get why or how it works no matter how much I stare at it. Can some one explain to me about how I'm getting the output? https://code.sololearn.com/WW3QwZhnn4MZ/?ref=app

18th Mar 2018, 2:07 PM
Babydoll Scripts
Babydoll Scripts - avatar
1 Réponse
0
Well document.getElementById is just getting the text that you’re changing. Then, you get whatever text is stored (“Text here”) in the paragraph that you just retrieved and put it in a. a is just a temp variable to allow you to switch the lines of text. Then you assign whatever text is in result1 to the paragraph’s text (which is what inner html is.) Then, you put a’s value into result1. The values stay the same for the next time you press he button, so it’s like they switch. Your code is a more elaborate version of switching variables like this: temp = x; x = y; y = temp; a is temp, result1 is y, and the paragraph’s text is x.
18th Mar 2018, 3:00 PM
Ariela
Ariela - avatar