Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 574a446

Browse files
committed
🎉 Add questions 4,5,6,7,8,9,10,11,12,13
1 parent 3df0d64 commit 574a446

File tree

1 file changed

+240
-0
lines changed

1 file changed

+240
-0
lines changed

‎Readme.md

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,243 @@ const sortedAges = students
111111
[Submit your solution](https://github.com/MenaiAla/javascript-interview-coding-questions-2023/pulls)
112112

113113
</details>
114+
115+
4. What is the output? and why?
116+
117+
```javascript
118+
let variable;
119+
console.log(typeof variable);
120+
```
121+
122+
<details>
123+
<summary>
124+
Solution
125+
</summary>
126+
<br>
127+
128+
Output: `undefined`.
129+
130+
Because we **defined** the variable without **assigning** it any value, and the default value is `undefined`.
131+
132+
</details>
133+
134+
5. What is the output? and why?
135+
136+
```javascript
137+
let variable = null;
138+
console.log(typeof variable);
139+
```
140+
141+
<details>
142+
<summary>
143+
Solution
144+
</summary>
145+
<br>
146+
147+
Output: `object`.
148+
149+
JavaScript has **7 primitive types**. All primitive types , except `null` can be tested with `typeof` operator.
150+
`typeof null` returns `object`. If we want to check for `null` we have to use `===null`.
151+
152+
| Type | `typeof` return | Wrapper |
153+
| --------- | --------------- | ------- |
154+
| Null | `object` | N/A |
155+
| Undefined | `undefined` | N/A |
156+
| Boolean | `boolean` | Boolean |
157+
| Number | `number` | Number |
158+
| BigInt | `bigint` | BigInt |
159+
| String | `string` | String |
160+
| Symbol | `symbol` | Symbol |
161+
162+
</details>
163+
164+
6. What is the output? and why?
165+
166+
```javascript
167+
console.log(variable);
168+
let variable = 1;
169+
```
170+
171+
<details>
172+
<summary>
173+
Solution
174+
</summary>
175+
<br>
176+
177+
Output: `ReferenceError: Cannot access 'variable' before initialization`.
178+
179+
Javascript has a default behavior which is moving the declaration to the top of the current scope (script or function). We call this behavior **Hoisting**.
180+
181+
In JavaScript we can declare a variable after it has been used. Hoisting is ofter considered a feature of `var` declarations.
182+
183+
However, we can consider the following behaviors as Hoisting:
184+
185+
| Hoisting | Behavior |
186+
| ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
187+
| Value | Being able to use a variable's value in its scope before the line it is declared |
188+
| Declaration | Being able to reference a variable in its scope before the line it is declared, without throwing a `ReferenceError`, but the value is always `undefined`. |
189+
190+
`let` allows you to declare variables that are limited to the scope of a `block` statement, or expression on which it is used, unlike the `var` keyword, which declares a variable globally, or locally to an entire function regardless of block scope. The other difference between `var` and `let` is that the latter can only be accessed after its declaration is reached.
191+
192+
For this reason, `let` declarations are commonly regarded as non-hoisted.
193+
194+
</details>
195+
196+
7. What is the output? and why?
197+
198+
```javascript
199+
console.log(variable);
200+
variable = 1;
201+
```
202+
203+
<details>
204+
<summary>
205+
Solution
206+
</summary>
207+
<br>
208+
209+
Output: `ReferenceError: variable is not defined`.
210+
211+
Because we access a variable that **does not exist** in the scope. This exception occurs when there is a non-existent variable referenced somewhere.
212+
213+
In JavaScript we declare variables using the keywords:
214+
215+
1. `var`
216+
2. `let`
217+
3. `const`
218+
219+
</details>
220+
221+
8. Create a `counter` function which has `increment` and `getValue` functions.
222+
223+
<details>
224+
<summary>
225+
Solution
226+
</summary>
227+
228+
```javascript
229+
function counter() {
230+
let counter = 0;
231+
return {
232+
increment: function (value) {
233+
return counter + value;
234+
},
235+
getValue: function () {
236+
return counter;
237+
},
238+
};
239+
}
240+
```
241+
242+
</details>
243+
244+
9. Write a function `concatenate` which concatenates `firstName` and `lastName` (`concatenate(firstName)(lastName)`).
245+
246+
<details>
247+
<summary>
248+
Solution
249+
</summary>
250+
251+
```javascript
252+
const concatenate = (firstName) => (lastName) => firstName + ' ' + lastName;
253+
```
254+
255+
</details>
256+
257+
10. Write a `curry` function.
258+
259+
> **Function Currying** is a concept of breaking a function with many arguments into many functions with single argument in such a way, that the output is same. In other words, its a technique of simplifying a multi-valued argument function into single-valued argument multi-functions.
260+
261+
<details>
262+
<summary>
263+
Solution
264+
</summary>
265+
<br>
266+
The concept of currying function is named after the <a href="https://en.wikipedia.org/wiki/Haskell_Curry">Haskell Brooks Curry</a> who developed it.
267+
268+
> By definition, Currying is a process of transforming a function with higher arity to a function with lower arity. Curry functions are higher order functions which takes a function as input and returns a function that accepts arguments of the input function and either invokes that function returning its result (if at least arity number of arguments have been provided) or returns a function that accepts the remaining arguments and so on
269+
270+
`curry :: ((a,b)=>c) -> a -> b -> c`
271+
272+
```javascript
273+
const curry = (fn) => {
274+
const arity = fn.length;
275+
return function f1(...args) {
276+
if (args.length >= arity) {
277+
return fn(...args);
278+
} else {
279+
return function f2(...moreArgs) {
280+
return f1(...args.concat(moreArgs));
281+
};
282+
}
283+
};
284+
};
285+
```
286+
287+
</details>
288+
289+
11. Write a function that gets an array and an element and returns an array with this element.
290+
291+
<details>
292+
<summary>
293+
Solution
294+
</summary>
295+
296+
```javascript
297+
// Using push method ( not recommended )
298+
const pushElement = (arr, el) => {
299+
arr.push(el);
300+
return arr;
301+
};
302+
// Using spread operator
303+
const pushElement = (arr, el) => [...arr, el];
304+
console.log(pushElement([1, 2, 3], 4));
305+
```
306+
307+
</details>
308+
309+
12. Write a function that merges two arrays and returns an array.
310+
311+
<details>
312+
<summary>
313+
Solution
314+
</summary>
315+
316+
```javascript
317+
// Using push method ( not recommended )
318+
const mergeArrays = (arr1, arr2) => {
319+
arr1.push(...arr2);
320+
return arr1;
321+
};
322+
// Using spread operator
323+
const mergeArrays = (arr1, arr2) => [...arr1, ...arr2];
324+
console.log(mergeArrays([1, 2, 3], [4, 5, 6]));
325+
```
326+
327+
</details>
328+
329+
13. Remove all duplicates from the given array:
330+
331+
```javascript
332+
const array = [1, 2, 2, 2, 3, 3, 4, 5, 8, 1, 10, 4, 9, 6];
333+
```
334+
335+
<details>
336+
<summary>
337+
Solution
338+
</summary>
339+
340+
```javascript
341+
// Using forEach and includes
342+
const removeDuplicates = (arr) => {
343+
let noDuplicates = [];
344+
arr.forEach((el) => !noDuplicates.includes(el) && noDuplicates.push(el));
345+
return noDuplicates;
346+
};
347+
// Using Set
348+
const removeDuplicates = (arr) => [...new Set(arr)];
349+
```
350+
351+
[Submit your solution](https://github.com/MenaiAla/javascript-interview-coding-questions-2023/pulls)
352+
353+
</details>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /