Timer Questions Flashcards

1
Q
for (let i = 1; i <= 5; i++) {
	console.log(i);
	setTimeout(function() {
	  console.log(i); 
	}, 1000);
}
A
for (let i = 1; i <= 5; i++) {
	console.log(i); // 1. >> 1, 2, 3, 4, 5
	setTimeout(function() {
	  console.log(i); // 2. >> 1, 2, 3, 4, 5 
	}, 1000);
}

The code will log 1, 2, 3, 4, and 5 simultaneously after a 1-second delay for all of them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
for (let i = 1; i <= 5; i++) {
	console.log(i);
	setTimeout(function timer() {
		console.log(i); 
	}, i * 1000);
}
A
for (let i = 1; i <= 5; i++) {
	console.log(i); // 1. >> 1, 2, 3, 4, 5
	setTimeout(function timer() {
		console.log(i); // 2. >> 1, 2, 3, 4, 5
	}, i * 1000);
}

This code will log the numbers 1, 2, 3, 4, and 5, each with a delay of 1, 2, 3, 4, and 5 seconds, respectively.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
let i;
for (i = 1; i <= 5; i++) {
	console.log(i);
	setTimeout(function() {
		console.log(i);
	}, 1000);
}
console.log(i);
A
let i; // 1. >> 1
for (i = 1; i <= 5; i++) {
	console.log(i); // 2. >> 1, 2, 3, 4, 5
	setTimeout(function() {
		console.log(i); // 4. >> 6, 6, 6, 6, 6 after 1 second
	}, 1000);
}
console.log(i); // 3. >> 6
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
let ii;
for (ii = 1; ii <= 5; ii++) {
	console.log(ii);
	setTimeout(function timer() {
    	console.log(ii);
  }, ii * 1000);
}
console.log(ii);
A
let ii; // 1.
for (ii = 1; ii <= 5; ii++) {
	console.log(ii); // 2. >> 1, 2, 3, 4, 5
	setTimeout(function timer() {
    	console.log(ii); // >> 4. 6, 6, 6, 6, 6  etc The code will log 6 after 1 second, 6 after 2 seconds etc
  }, ii * 1000);
}
console.log(ii); // 3. >> 6
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
let iii;
for (iii = 1; iii <= 5; iii++) {
	console.log(iii);
	setTimeout(function timer() {
    	console.log(iii);
  }, iii * 1000);
}
iii=300;
console.log(iii);
A
let iii; // 1.
for (iii = 1; iii <= 5; iii++) {
	console.log(iii); // 2. >> 1, 2, 3, 4, 5
	setTimeout(function timer() {
    	console.log(iii); // >> 4. 300, 300, 300, 300, 300 etc The code will log 300 after 1 second, 300 after 2 seconds etc
  }, iii * 1000);
}
iii=300;
console.log(iii); // 3. >> 6
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
function x(){
  for (var i = 1; i <= 10; i++){
    setTimeout(function (){
    console.log(i);
    }, i * 1000);
  }
  console.log("Learn")
}
x();
A
  1. Learn
  2. 11,11,11,11,11,11,11,11,11,
  3. var is global
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
function x(){
	i=4;
	console.log(i);
	for (var i = 1; i <= 10; i++){
	  console.log(i); 
	  setTimeout(function (){
	  	console.log(i); 
	  }, i * 1000);
	}
	console.log("Learn");
  }
  x();
A
function x(){
	i=4 
	console.log(i); // 1. >>4
	for (var i = 1; i <= 10; i++){
	  console.log(i); // 2. >> 1,2,3,4,5,6,7,8,9,10
	  setTimeout(function (){
	  	console.log(i); // 4. 11 * 10 times
	  }, i * 1000);
	}
	console.log("Learn")// 3. >> Learn
  }
  x();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
function x(){
	for (let i = 1; i <= 10; i++){
		console.log(i);
		setTimeout(function (){
	  		console.log(i);
	  	}, i * 1000);
	}
	console.log("Learn")
}
x();
A
function x(){
	for (let i = 1; i <= 10; i++){
		console.log(i); // 2. 1,2,3,4,5,6,7,8,9,10
		setTimeout(function (){
	  		console.log(i); // 3. 1,2...10 after 1, 2..10 second space between them
	  	}, i * 1000);
	}
	console.log("Learn"); //1. Learn
  }
  x();
  1. When loop started with i = 1 , function formed a closure with new variable itself.
  2. After i++ , when function formed closure again, it does have new variable with value 2 and saves it.
  3. And so on. This way it forms it makes 10 copies of variable i and forms closure with each and every function.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
function x(){
	for (let i = 1; i <= 10; i++){
	  function close(i){
		setTimeout(function (){
		console.log(i);
		}, i * 1000);
	  }
	  close(i);  
	}
	console.log("Learn")
  }
  x();
A
function x(){
	for (let i = 1; i <= 10; i++){
	  console.log("1. ", i); // 1. >> 1,2...10
	  function close(i){
		console.log("2. ", i); // 2. >> 1,2...10
		setTimeout(function (){
			console.log("4. ", i); // 4. >> 1,2...10 spaced out by 1 sedond
		}, i * 1000);
	  }
	  close(i);  
	}
	console.log("3. ", "Learn")// 3. >> Learn
  }
  x();

Because every time you call close() function with i , it creates a new copy of i for setTimeout()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly