The source code for this blog is available on GitHub.
Note
Top

What you need to prepare for a JavaScript interview

Cover Image for What you need to prepare for a JavaScript interview
Chen Han
Chen Han

Javascript

Here are some common questions that may be asked in a JavaScript interview:

  1. What is JavaScript and what is it used for?

  2. What is the difference between var, let, and const?

  3. How do you declare a function in JavaScript?

  4. What is the difference between null and undefined in JavaScript?

  5. What is the difference between == and ===?

  6. How do you handle exceptions in JavaScript?

  7. 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 called innerFunction and returns it. The innerFunction has access to the x parameter of the outerFunction, even after the outerFunction has returned. When the closure variable is defined and assigned the return value of the outerFunction, it becomes a closure that has access to the x parameter of the outerFunction. When the closure is called, it logs the value of x 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 called count and returns a function that increments count and logs it to the console. When the counter variable is defined and assigned the return value of the createCounter function, it becomes a closure that has access to the count variable of the createCounter function. When the counter function is called multiple times, it increments the value of count 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 the count variable of the createCounter function, even after the createCounter function has finished executing. This allows the counter 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.

  8. What is the prototype property in JavaScript and what is it used for?

  9. What is an event loop in JavaScript?

  10. 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  --&gt;  1 + 6 = 7
   942  --&gt;  9 + 4 + 2 = 15  --&gt;  1 + 5 = 6
132189  --&gt;  1 + 3 + 2 + 1 + 8 + 9 = 24  --&gt;  2 + 4 = 6
493193  --&gt;  4 + 9 + 3 + 1 + 9 + 3 = 29  --&gt;  2 + 9 = 11  --&gt;  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 ;
}
© 2024 WOOTHINK. All Rights Reserved.
Site MapTerms and ConditionsPrivacy PolicyCookie Policy