Skip to content

Commit 99cc328

Browse files
macmac
authored andcommitted
initial commit
1 parent ce04cdc commit 99cc328

File tree

2 files changed

+264
-9
lines changed

2 files changed

+264
-9
lines changed

README.md

Lines changed: 263 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,269 @@ The first line creates a timer that will call the function every 2 seconds. The
8383

8484
- The `clearInterval()` method clears a timer set with the setInterval() method.
8585

86-
- The ID value returned by setInterval() is used as the parameter for the `clearInterval()` method.
86+
- The ID value returned by `setInterval()` is used as the parameter for the `clearInterval()` method.
8787

88+
---------------------------------------
89+
---------------------------------------
90+
# Question 3
91+
## Write printAnimals() in such a way that it prints all animals in the object below.
92+
```js
93+
const animals = [ { species: 'Lion', name: 'King' },
94+
{ species: 'Whale', name: 'Queen' } ];
95+
96+
function printAnimals(i) {
97+
this.print = function() {
98+
console.log('#' + i + ' ' + this.species + ': ' + this.name);
99+
}
100+
this.print();
101+
102+
}
103+
// Code
104+
105+
const animals = [ { species: 'Lion', name: 'King' },
106+
{ species: 'Whale', name: 'Queen' } ];
107+
108+
function printAnimals() {
109+
let i = 1;
110+
animals.forEach(function(animal){
111+
this.print = function(){
112+
console.log('#' + i++ + ' ' + animal.species + ': ' + animal.name);
113+
}
114+
this.print();
115+
}.bind(this)
116+
)
117+
118+
}
119+
printAnimals();
120+
121+
// Output :
122+
#1 Lion: King
123+
#2 Whale: Queen
124+
```
125+
* Explantion - For a given function, creates a bound function that has the same body as the original function. The this object of the bound function is associated with the specified object, and has the specified initial parameters.
126+
* This code creates a `printAnimals` fn that iterates over the `animals` array using `forEach` method,
127+
inside the loop we define a `print` fn using the `bind` method to ensure that `this` refers to corect object.
128+
In last we call the `printAnimals()` fn to print all the elements in array.
129+
130+
---------------------------------------
131+
---------------------------------------
132+
# Question 4
133+
## What is the output & Why?
134+
```js
135+
function sumOfNumbers() {
136+
var total = 0;
137+
for (var i = 0; i < arguments.length; i++) {
138+
total += arguments[i];
139+
}
140+
return total;
141+
}
142+
var numbers = [1, 2, 3];
143+
var sum = sumOfNumbers.apply(null, numbers);
144+
console.log(sum);
145+
146+
// Output 6 :
147+
6
148+
149+
```
150+
* Why - Because `sumOfNumbers` fn takes an argu and return their sum . The code calls this fn using `apply` method , paasing an array of no. as arguments, the fn return the sum of these no. which is 6
151+
and then finally value logged in the console.
152+
153+
---------------------------------------
154+
---------------------------------------
155+
# Question 5
156+
## What is the output & Why?
157+
```js
158+
"use strict";
159+
var person = {
160+
name: "Jack",
161+
prop: {
162+
name: "Daniel",
163+
getName: function () {
164+
return this.name;
165+
},
166+
},
167+
};
168+
var name = person.prop.getName.bind(person);
169+
console.log(name());
170+
171+
var name = person.prop.getName();
172+
console.log(name);
173+
174+
// Output
175+
Jack
176+
Daniel
177+
```
178+
- Why - Because the code create an object `person` with properties `name` and `prop`, the first
179+
log output `"Jack"` and second `"Daniel"`
180+
- Object called `person` with a property `name` set to `"jack"` and another property `prop` which is an object with a propert `name` set to `"Daniel"` and a method `getName` that returns the value of `this.name`.
181+
---------------------------------------
182+
---------------------------------------
183+
# Question 6
184+
## What is the output & Why?
185+
```js
186+
function makeUser()
187+
{ return { name: "John", ref: this };
188+
} l
189+
let user = makeUser();
190+
alert( user.ref.name );
88191

192+
// Output
193+
undefined
194+
195+
```
196+
* Why - This code defines a function makeUser that returns an object with properties name and ref. The name property is set to the string "John", while the ref property is set to the value of this inside the function.
197+
198+
When the function is called with let user = makeUser();, it returns the object and assigns it to the variable user. since the function is called as a regular function (not as a method of an object or with the new keyword), the value of this inside the function is the global object .
199+
200+
At last in result, when the code run it try to access user.ref.name, it is actually trying to access window.name, which is undefined. This causes the alert statement to throw a TypeError, because it’s trying to access the property name of an undefined value.
201+
202+
---------------------------------------
203+
---------------------------------------
204+
# Question 7
205+
## What is the output & Why?
206+
```js
207+
const func = (function (a) {
208+
delete a;
209+
return a;
210+
})(5);
211+
console.log(func);
212+
213+
// Output:
214+
5
215+
```
216+
* Why - Because code defines a self-invoking function that takes an argument `a`, attempts to delete it, and returns its value. The delete operator has no effect on `a`, so the function returns the original value of` a`, which is `5`. The output is `5`.
217+
- `Self-invoking fn ` --> `A self-invoking function is a function that runs automatically as soon as it is created. It’s useful for organizing code and keeping variables private.`
218+
219+
---------------------------------------
89220
---------------------------------------
221+
# Question 8
222+
## Create an object `calculator` with three methods: - `read()` prompts for two values and saves them as object properties with names `a` and `b` respectively. - `sum()` returns the sum of saved values. - `mul()` multiplies saved values and returns the result.
223+
* Ans
224+
```js
225+
const calculator = {
226+
read() {
227+
this.a = +prompt('Enter the first no:', );
228+
this.b = +prompt('Enter the second no:', );
229+
},
230+
sum() {
231+
return this.a + this.b;
232+
},
233+
mul() {
234+
return this.a * this.b;
235+
}
236+
};
237+
calculator.read()
238+
alert("Sum of Both no.",calculator.sum())
239+
alert("Multiply of Both no.", calculator.mul())
240+
241+
// Output :
242+
Enter the first no:1
243+
Enter the second no:1
244+
Sum of Both no. 2
245+
Multiply of Both no. 1
246+
247+
```
248+
---------------------------------------
249+
---------------------------------------
250+
# Question 9
251+
## What is scope chain and lexical scoping in JS?
252+
253+
* Answer - `Scope chain` is a list of all the scopes that are currently in effect like when, a variable is declared, it is added to the scope chain. When a variable is accessed, the JavaScript engine searches the scope chain to find the variable.
254+
* `Lexical scoping` is a method of determining the scope of a variable based on its declaration like we see In `lexical scoping`, the scope of a variable is determined by the block of code in which it is declared.
255+
256+
For Eg.
257+
```js
258+
const globalVar = 'global';
259+
260+
function outerFunc() {
261+
const outerVar = 'outer';
262+
263+
function innerFunc() {
264+
const innerVar = 'inner';
265+
console.log(globalVar); // 'global'
266+
console.log(outerVar); // 'outer'
267+
console.log(innerVar); // 'inner'
268+
}
269+
270+
innerFunction();
271+
}
272+
273+
outerFunction();
274+
275+
/// Output:
276+
global
277+
outer
278+
inner
279+
280+
```
281+
* In this eg, `globalVar` is defined in the global scope, so it is visible and accessible from anywhere in the code. `outerVar` is defined inside `outerFunc`, so it is only visible within that function and any nested functions. `innerVar` is defined inside `innerFunc`, so it is only visible within that function.
282+
283+
* When `innerFunc` is called, it logs the values of all three variables. Since `innerVar` is defined within `innerFunc`, it can be accessed directly. To access `outerVar`, the JavaScript engine must go up one level in the scope chain to `outerFunc`. To access `globalVar` ,it must go up two levels to the global scope.
284+
285+
---------------------------------------
286+
---------------------------------------
287+
# Questio 10
288+
## What is FEC & GEC? How are Execution Contexts Created?
289+
* Answer - As we know There are two main types of Execution Contexts in JavaScript:
290+
1.`Global Execution Context (GEC) `
291+
2.`Function Execution Context (FEC)`
292+
293+
* The `GEC` is the default Execution Context created by the JavaScript engine when it receives a script file. It is the base environment where all JavaScript code that is not inside a function is executed. For every JavaScript file, there can only be one GEC.
294+
* The `FEC` is a different type of Execution Context created by the JavaScript engine whenever a function is called. It is created within the `GEC` to evaluate and execute the code within that function
295+
and every function call gets its own `FEC`, there can be more than one `FEC` in the run-time of a script.
296+
297+
When a function is called, the JavaScript engine creates a new `FEC` for that function and pushes it onto the top of the execution stack. The code within the function is then executed within this new `FEC`. When the function returns, its `FEC `is popped off the execution stack, and control returns to the calling context.
298+
299+
---------------------------------------
300+
---------------------------------------
301+
# Question 11
302+
## Write a JavaScript program to create a clock.
303+
304+
* Program
305+
306+
---------------------------------------
307+
```js
308+
function clock() {
309+
let date = new Date.options();
310+
311+
let hr = date.getHours().toString();
312+
let min = date.getMinutes();
313+
let sec = date.getSeconds();
314+
let time = hr + ":" + min + ":" + sec
315+
console.log(time);
316+
}
317+
318+
setInterval(clock, 1000);
319+
320+
```
321+
---------------------------------------
322+
323+
# Question 12
324+
Make the following code work
325+
326+
```js
327+
328+
[1, 2, 3, 4, 5, 6].shuffle();
329+
330+
// Working Code
331+
332+
Array.prototype.shuffle = function() {
333+
let arr = []
334+
for (let i = arr.length - 1; i > 0; i--) {
335+
let j = Math.floor(Math.random() * (i + 1));
336+
[arr[i], arr[j]] = [arr[j], arr[i]];
337+
}
338+
console.log(arr);
339+
};
340+
[1, 2, 3, 4, 5, 6].shuffle();
341+
342+
// Output:
343+
[ 6, 1, 5, 2, 3, 4 ]
344+
345+
```
346+
* The `Math.floor() `is static method always rounds down and returns the largest integer less than or equal to a given number.
347+
* The `Math.random()` is static method which returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1, with approximately uniform distribution over that range.
348+
349+
350+
---------------------------------------
351+

index.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1 @@
1-
let timerId = setInterval(function () {
2-
console.log("tick")
3-
}, 2000);
4-
5-
setTimeout(function () {
6-
clearInterval(timerId);
7-
console.log("stop");
8-
}, 5000);
1+
// In Readme

0 commit comments

Comments
 (0)