|
| 1 | +# Tasks |
| 2 | + |
| 3 | +Please complete the following tasks and post them on the tapaScript Discord under "40 Days of JavaScript". |
| 4 | + |
| 5 | +> **DO NOT USE AI to FIND ANSWERS**. If you are stuck, let's discuss it on DISCORD and learn. Also, please note that none of the answers need you to create any UI. Just focus on the logic building and print the output on the browser console. |
| 6 | +
|
| 7 | +## 1. What's the output of the code below? |
| 8 | + |
| 9 | +```js |
| 10 | +function f1() { |
| 11 | + console.log('f1'); |
| 12 | +} |
| 13 | + |
| 14 | +function f2() { |
| 15 | + console.log('f2'); |
| 16 | +} |
| 17 | + |
| 18 | +function f3() { |
| 19 | + console.log('f3'); |
| 20 | +} |
| 21 | + |
| 22 | +function f4() { |
| 23 | + console.log('f4'); |
| 24 | +} |
| 25 | + |
| 26 | +console.log("Let's do it!"); |
| 27 | + |
| 28 | +setTimeout(function() {f1();}, 0); |
| 29 | + |
| 30 | +f4(); |
| 31 | + |
| 32 | +setTimeout(function() {f2();}, 5000); |
| 33 | + |
| 34 | +setTimeout(function() {f3();}, 3000); |
| 35 | +``` |
| 36 | + |
| 37 | +Options are, |
| 38 | + |
| 39 | +- Let's do it!, f4, f1, f3, f2 |
| 40 | +- Let's do it!, f1, f3, f2, f4 |
| 41 | +- Let's do it!, f1, f2, f3, f4 |
| 42 | +- Let's do it!, f1, f4, f2, f3 |
| 43 | + |
| 44 | +## Example Answer: Let's do it!, f4, f1, f3, f2 |
| 45 | + |
| 46 | +Explanation: |
| 47 | + |
| 48 | +- "Let's do it!" is executed by Execution Stack |
| 49 | +- f1() calls browser API, so gets added to Callback Queue |
| 50 | +- f4() gets added to Execution Stack and is executed |
| 51 | +- Event loop finds a callback function f1() in callback queue & executes it |
| 52 | +- f2() calls browser API and gets added to Callback Queue. Similarly f3() is added to callback queue |
| 53 | +- Now there is nothing in Execution Stack, so event loop checks & finds f2() & - f3() callback functions in callback queue |
| 54 | +- f3() goes back into the stack after timeout, and gets executed |
| 55 | +- f2() too goes back into the stack after timeout, and gets executed |
| 56 | + |
| 57 | +## 2. What's the output of the code below? |
| 58 | + |
| 59 | +```js |
| 60 | +function f1() { |
| 61 | + console.log('f1'); |
| 62 | +} |
| 63 | + |
| 64 | +console.log("Let's do it!"); |
| 65 | + |
| 66 | +setTimeout(function() {console.log('in settimeout');}, 0); |
| 67 | + |
| 68 | +f1(); |
| 69 | +f1(); |
| 70 | +f1(); |
| 71 | +f1(); |
| 72 | +``` |
| 73 | + |
| 74 | +Options are, |
| 75 | + |
| 76 | +- Let's do it!, in settimeout, f1, f1, f1, f1 |
| 77 | +- Let's do it!, f1, f1, f1, f1, in settimeout |
| 78 | +- Let's do it!, f1, , in settimeout, f1, f1, f1 |
| 79 | + |
| 80 | +## 3. Which statements are `true`? Select multiple |
| 81 | + |
| 82 | +- [ ] JavaScript is single-threaded |
| 83 | +- [ ] By default, JavaScript is synchronous |
| 84 | +- [ ] Only promises make JavaScript asynchronous |
| 85 | +- [ ] All function callbacks are asynchronous |
| 86 | + |
| 87 | +## 4. Which statement is `true`? Select Only one |
| 88 | + |
| 89 | +- (_) JavaScript Function Execution Stack(Call Stack) never gets empty. |
| 90 | +- (_) The job queue gets higher priority than the callback queue. |
| 91 | +- (_) The only job of Event Loop is to manage the Call Stack |
| 92 | +- (_) The StackOverflow exception is random. |
| 93 | + |
| 94 | +### 5. Guess the output |
| 95 | + |
| 96 | +```js |
| 97 | +const tom = () => console.log('Tom'); |
| 98 | + |
| 99 | +const jerry = () => console.log('Jerry'); |
| 100 | + |
| 101 | +const cartoon = () => { |
| 102 | + console.log('Cartoon'); |
| 103 | + |
| 104 | + setTimeout(tom, 5000); |
| 105 | + |
| 106 | + new Promise((resolve, reject) => |
| 107 | + resolve('should it be right after Tom, before Jerry?') |
| 108 | + ).then(resolve => console.log(resolve)) |
| 109 | + |
| 110 | + jerry(); |
| 111 | +} |
| 112 | + |
| 113 | +cartoon(); |
| 114 | +``` |
| 115 | + |
| 116 | +Options are, |
| 117 | + |
| 118 | +- Cartoon, Jerry, should it be right after Tom, before Jerry?, tom |
| 119 | +- Cartoon, Tom, Jerry, should it be right after Tom, before Jerry?, |
| 120 | +- Cartoon, Tom, should it be right after Tom, before Jerry?, Jerry |
| 121 | +- Error |
| 122 | + |
| 123 | +### 6. Guess the output |
| 124 | + |
| 125 | +```js |
| 126 | +const tom = () => console.log('Tom'); |
| 127 | +const jerry = () => console.log('Jerry'); |
| 128 | +const doggy = () => console.log('Doggy'); |
| 129 | + |
| 130 | +const cartoon = () => { |
| 131 | + console.log('Cartoon'); |
| 132 | + |
| 133 | + setTimeout(tom, 50); |
| 134 | + setTimeout(doggy, 30); |
| 135 | + |
| 136 | + new Promise((resolve, reject) => |
| 137 | + resolve('I am a Promise, right after tom and doggy! Really?') |
| 138 | + ).then(resolve => console.log(resolve)); |
| 139 | + new Promise((resolve, reject) => |
| 140 | + resolve('I am a Promise after Promise!') |
| 141 | + ).then(resolve => console.log(resolve)); |
| 142 | + |
| 143 | + jerry(); |
| 144 | +} |
| 145 | + |
| 146 | +cartoon(); |
| 147 | +``` |
| 148 | + |
| 149 | +Options are, |
| 150 | + |
| 151 | +- Cartoon, Jerry, I am a Promise, right after tom and doggy! Really?, I am a Promise after Promise!, , Tom, Doggy |
| 152 | +- Cartoon, Jerry, I am a Promise after Promise!, I am a Promise, right after tom and doggy! Really?, Doggy, Tom |
| 153 | +- Cartoon, Jerry, I am a Promise, right after tom and doggy! Really?, I am a Promise after Promise!, Doggy, Tom |
| 154 | +- Cartoon, Tom, Doggy, I am a Promise, right after tom and doggy! Really?, I am a Promise after Promise!, Jerry |
| 155 | +- None of the above. |
| 156 | + |
| 157 | +### 7. Guess the output |
| 158 | + |
| 159 | +```js |
| 160 | +const f1 = () => console.log('f1'); |
| 161 | +const f2 = () => console.log('f2'); |
| 162 | +const f3 = () => console.log('f3'); |
| 163 | +const f4 = () => console.log('f4'); |
| 164 | + |
| 165 | +f4(); |
| 166 | + |
| 167 | +setTimeout(f1, 0); |
| 168 | + |
| 169 | +new Promise((resolve, reject) => { |
| 170 | + resolve('Boom'); |
| 171 | +}).then(result => console.log(result)); |
| 172 | + |
| 173 | +setTimeout(f2, 2000); |
| 174 | + |
| 175 | +new Promise((resolve, reject) => { |
| 176 | + resolve('Sonic'); |
| 177 | +}).then(result => console.log(result)); |
| 178 | + |
| 179 | +setTimeout(f3, 0); |
| 180 | + |
| 181 | +new Promise((resolve, reject) => { |
| 182 | + resolve('Albert'); |
| 183 | +}).then(result => console.log(result)); |
| 184 | +``` |
| 185 | + |
| 186 | +Options are, |
| 187 | + |
| 188 | +- f4, Boom, Sonic, Albert, f1, f3, f2 |
| 189 | +- f4, f1, Boom, f2, Sonic, f3, Albert |
| 190 | +- f4, Boom, Sonic, Albert, f3, f1, f2 |
| 191 | +- f4, Boom, Sonic, Albert, f1, f2, f3 |
| 192 | + |
| 193 | +### 8. Guess the output |
| 194 | + |
| 195 | +```js |
| 196 | +const f1 = () => { |
| 197 | + console.log('f1'); |
| 198 | + f2(); |
| 199 | +} |
| 200 | +const f2 = () => console.log('f2'); |
| 201 | +const f3 = () => console.log('f3'); |
| 202 | +const f4 = () => console.log('f4'); |
| 203 | + |
| 204 | +f4(); |
| 205 | + |
| 206 | +setTimeout(f1, 0); |
| 207 | + |
| 208 | +new Promise((resolve, reject) => { |
| 209 | + resolve('Sonic'); |
| 210 | +}).then(result => console.log(result)); |
| 211 | + |
| 212 | +setTimeout(f3, 0); |
| 213 | + |
| 214 | +new Promise((resolve, reject) => { |
| 215 | + resolve('Albert'); |
| 216 | +}).then(result => console.log(result)); |
| 217 | +``` |
| 218 | + |
| 219 | +Options are, |
| 220 | + |
| 221 | +- f4, f1, f2, Sonic, f3, Albert |
| 222 | +- f4, Sonic, Albert, f3, f1, f2 |
| 223 | +- f4, Sonic, Albert, f1, f2, f3 |
| 224 | +- f4, Albert, Sonic, f1, f2, f3 |
0 commit comments