What you need to prepare for a JavaScript interview
Javascript
Here are some common questions that may be asked in a JavaScript interview:
-
What is JavaScript and what is it used for?
-
What is the difference between var, let, and const?
-
How do you declare a function in JavaScript?
-
What is the difference between null and undefined in JavaScript?
-
What is the difference between == and ===?
-
How do you handle exceptions in JavaScript?
-
What is a closure in JavaScript?
In JavaScript, a closure is a function that has access to the variables and parameters of its outer function, even after the outer function has finished executing. Closures are created when a function is defined inside another function, and they allow the inner function to access the variables and parameters of the outer function even after the outer function has returned.
Here is an example of a closure in JavaScript:
function outerFunction(x) { // x is a parameter of the outer function return function innerFunction() { // The inner function has access to the x parameter of the outer function console.log(x); } } const closure = outerFunction('hello'); // The inner function can be called even after the outer function has finished executing closure(); // Outputs 'hello'
In this example, the
outerFunction
defines a function calledinnerFunction
and returns it. TheinnerFunction
has access to thex
parameter of theouterFunction
, even after theouterFunction
has returned. When theclosure
variable is defined and assigned the return value of theouterFunction
, it becomes a closure that has access to thex
parameter of theouterFunction
. When theclosure
is called, it logs the value ofx
to the console.Overall, closures are an important concept in JavaScript because they allow inner functions to access the variables and parameters of their outer functions even after the outer functions have finished executing. They are often used to create functions that have a specific context or scope, and they can be useful for implementing various design patterns and techniques in JavaScript.
here is another example of a closure in JavaScript:
function createCounter() { let count = 0; return function() { count++; console.log(count); } } const counter = createCounter(); counter(); // Outputs 1 counter(); // Outputs 2 counter(); // Outputs 3
In this example, the
createCounter
function defines a variable calledcount
and returns a function that incrementscount
and logs it to the console. When thecounter
variable is defined and assigned the return value of thecreateCounter
function, it becomes a closure that has access to thecount
variable of thecreateCounter
function. When thecounter
function is called multiple times, it increments the value ofcount
and logs it to the console on each call.This example shows how closures can be used to create functions that have a specific context or scope. In this case, the
counter
function has access to thecount
variable of thecreateCounter
function, even after thecreateCounter
function has finished executing. This allows thecounter
function to maintain its own internal state and keep track of the number of times it has been called.Overall, closures are an important and powerful concept in JavaScript that allow inner functions to access the variables and parameters of their outer functions even after the outer functions have finished executing. They are often used to create functions with a specific context or scope, and they can be useful for implementing various design patterns and techniques in JavaScript.
-
What is the prototype property in JavaScript and what is it used for?
-
What is an event loop in JavaScript?
-
What is a callback function in JavaScript and how is it used?
These are just a few examples of the types of questions that may be asked in a JavaScript interview. It's a good idea to familiarize yourself with the language and its features, as well as common design patterns and best practices, in order to prepare for a JavaScript interview.
What is "泛型" in javascript
"泛型" is the Chinese word for "generics." In programming, generics refer to a way to design a class, interface, or function that can work with a variety of data types, rather than being specific to one data type.
In JavaScript, generics are implemented using type parameters. For example, you might define a generic function like this:
function identity<T>(arg: T): T {
return arg;
}
In this example, the type parameter T
allows the identity
function to accept an argument of any data type and return a value of the same data type. You can call the function using any data type, like this:
let output = identity<string>("hello"); // output is of type string
let output = identity<number>(123); // output is of type number
Generics can be used to create reusable and flexible code that can work with a variety of data types. They are a powerful tool for writing type-safe code in JavaScript.
Async
針對javascript
演算法的部分
CallStack & Queue
Sort two sorted array
題目為將兩個已經排列的陣列再排列。下列演示三種可行的方法
// 使用reduce (未完)
function sortSortedArray (arr1, arr2) {
return arr1.reduce((acc, cur) => {
let ele = acc.find((element) => element >= cur )
console.log(ele, acc.indexOf(ele))
acc.splice(acc.indexOf(ele), 0, cur);
return acc
}, arr2)
}
const arr1 = [1, 4, 5, 6]
const arr2 = [1, 2, 3, 7, 8]
result = sortSortedArray(arr1, arr2)
console.log(result)
// 使用內建sort
function sortSortedArray (arr1, arr2) {
let mergedArray = [...arr1, ...arr2]
mergedArray.sort(function(a, b) {
return a - b;
})
return mergedArray
}
const arr1 = [1, 4, 5, 6]
const arr2 = [1, 2, 3, 7, 8, 100]
result = sortSortedArray(arr1, arr2)
console.log(result)
// 降低時間複雜度的解法(使用指標)
function sortSortedArray (arr1, arr2) {
let idx1 = 0 , idx2 = 0, result = [];
let haltLength = ((arr1 > arr2) ? arr1 : arr2).length
while(idx1 !== haltLength && idx2 !== haltLength) {
if (arr1[idx1] === arr2[idx2]) {
result = [...result, arr1[idx1], arr2[idx2]]
idx1++
idx2++
} else if (arr1[idx1] < arr2[idx2]) {
result = [...result, arr1[idx1]]
idx1++
} else if (arr1[idx1] > arr2[idx2]) {
result = [...result, arr2[idx2]]
idx2++
}
}
console.log(idx1, idx2)
if(idx1 !== arr1.length) {
result = [...result, ...arr1.slice(arr1[idx1])]
}
if(idx2 !== arr2.length) {
result = [...result, ...arr2.slice(idx2)]
}
return result
}
const arr1 = [1, 4, 5, 6]
const arr2 = [1, 2, 3, 7, 8, 100]
result = sortSortedArray(arr1, arr2)
console.log(result)
must know
Understand this usage break
, continue
in for loop
for (let i = 0; i < 10; i++) {
if (i === 3) { break; }
text += "The number is " + i + "<br>";
}
for (let i = 0; i < 10; i++) {
if (i === 3) { continue; }
text += "The number is " + i + "<br>";
}
How to use Object.entries
// array like object
const obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.entries(obj)); // [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]
// array like object with random key ordering
const anObj = { 100: 'a', 2: 'b', 7: 'c' };
console.log(Object.entries(anObj)); // [ ['2', 'b'], ['7', 'c'], ['100', 'a'] ]
Sum of Digits
Example
16 --> 1 + 6 = 7
942 --> 9 + 4 + 2 = 15 --> 1 + 5 = 6
132189 --> 1 + 3 + 2 + 1 + 8 + 9 = 24 --> 2 + 4 = 6
493193 --> 4 + 9 + 3 + 1 + 9 + 3 = 29 --> 2 + 9 = 11 --> 1 + 1 = 2
Solution
function reducer(n) {
const intN = parseInt(n);
let cur = Math.floor(intN / 10);
let remainder = intN % 10;
while(cur > 0) {
remainder += cur % 10;
cur = Math.floor(cur / 10);
console.log("cur:", cur, "remainder:", remainder);
}
return remainder;
}
function digital_root(n) {
console.log("original:", n);
let result = reducer(n);
while (result >= 10) {
result = reducer(result);
}
return result
}
function reducer(n) {
if (n === 0) return 0;
return (n % 10 + reducer(parseInt(n / 10)));
}
function digital_root(n) {
let result = reducer(n);
while (result >= 10) {
result = reducer(result)
}
return result ;
}