+ 1

Help me understand this code please?

So I got this code from somebody else. I want to be able to understand the Javascript. I understand the basics, but I don't understand why some variables are there, and I don't understand that if/else statement. https://code.sololearn.com/Wt3WHVtOF4ZT/?ref=app

11th Jun 2018, 7:51 PM
Daniel Cooper
Daniel Cooper - avatar
7 Answers
+ 2
// Created by Daniel Cooper // Created by Ben Allen (Njinx) window.onload = function() { //happens if the window is loaded var info = document.getElementById("print"); //the numbers will stay in this element var toggle = document.getElementById("toggle"); //the button's id var i = 0; //the number to be writed var btn = 1; //0=number will increase 1=do nothing interval = null; //the interval for increasing the number toggle.onclick = function () { //happens when you click the button if (btn) { //1 is true 0 is false (first time its true) btn = 0; //sets btn to 0 interval = setInterval(function() { //makes an interval. used to repeat //continueally until something //stopped the interval info.innerHTML = i++; //writes i and then adds 1 }, 1); //1 means every 1 millisecond } els
11th Jun 2018, 8:01 PM
Roel
Roel - avatar
+ 1
Thanks, but can you explain that if statement? if (btn) { } I'm assuming it's testing if it's true? Correct me if I'm wrong.
11th Jun 2018, 8:06 PM
Daniel Cooper
Daniel Cooper - avatar
+ 1
i said in the comment that its true when btn = 1, and false when btn = 0, but im not sure since i most of the time use [var name]{operant}(value to test){function}
11th Jun 2018, 8:12 PM
Roel
Roel - avatar
+ 1
Roel Can confirm its testing if it's true. btn set to 1 will be true. So I'm basically testing if the btn is equal to 1.
11th Jun 2018, 9:52 PM
Ben Allen (Njinx)
Ben Allen (Njinx) - avatar
0
explain using comments please.
11th Jun 2018, 7:51 PM
Daniel Cooper
Daniel Cooper - avatar
0
you can copy paste it in your javascript but my internet connection failed
11th Jun 2018, 8:02 PM
Roel
Roel - avatar
0
Ben Allen (Njinx) so i was right with my comment â˜ș good to know
12th Jun 2018, 4:57 AM
Roel
Roel - avatar