[Javascript] Basic Knowledge

Variables

Javascript data types; undefined, null, boolean, string, symbol, number, object

When JavaScript variables are declared, they have an initial value of undefined. If you do a mathematical operation on an undefined variable your result will be NaN which means Not a Number. If you concatenate a string with an undefined variable, you will get a literal string of "undefined".

Decimal
Not all real numbers can accurately be represented in floating point. This can lead to rounding errors

Length
To find the length of a string: string.length

Bracket Notation
To get the first character of the string string[0]

Immutable

In JavaScript, String values are immutable, which means that they cannot be altered once created. The only way to change string would be assign it with a new string.

1
2
var myStr = "Bob";
myStr = "Job";

Array

Arrays in JavaScript can contain multiple types of values.

The entries of arrays are mutable and can be changed freely.

1
2
3
4
5
6
let array = ["John", 23];
array.push("Alex");
array.push("Alice");
array.unshift("Daniel");
array.pop(); // Alice
array.shift(); // Daniel

To check if an array contains element:

1
arr.includes(element);

String

In ES6 new feature startsWith(), endsWith(), includes(), indexOf()

1
2
3
4
5
6
7
8
const str = "Saturday night plans"
str.startsWith('Sat'); // True

str.endsWith('plans'); // True

str.includes('night'); // True

str.indexOf("ght");

Interview

  1. Difference between forEach and map. *

map will return an array, forEach won’t.

  1. Difference between apply and call **

  2. Explain the concept of ES6 Promises to a 5-year-old *

  3. What are the advantages of using ES6 maps over objects? What about using ES6 sets over arrays?

  4. Algorithm question: excludeItems *

  5. Given 2 identical DOM trees(but not equal) and one element of the first DOM tree, how would you find this element in the second DOM tree?
    https://www.glassdoor.com/Interview/Given-two-identical-DOM-trees-not-the-same-one-and-a-node-from-one-of-them-find-the-node-in-the-other-one-QTN_970047.htm

  6. Write an array flatten function recursively and iteratively. The array can have multiple types. *******

    • In JavaScript, write a function that takes an array as input that can contain both ints and more arrays (which can also contain an array or int) and return the flattened array.
1
[1, [2, [ [3, 4], 5], 6]] => [1, 2, 3, 4, 5, 6]
  1. Write an emitter class **

    • Support subscribing to events
    • Support emitting events
    • Support unsubscribing existing subscriptions by releasing them
  2. == vs === *

  3. event delegation *

  4. Given a picture, how would you hide/show a child picture on hovering on this parent? ***

    • How would you ensure clicking on this picture would go to a specific link?
    • How would you ensure the child is positioned in the top right of the parent pictjure?
  5. Difference between array and object *

  6. One-way & Two-way binding

  7. Functions you can perform on strings *

  8. Difference between block and inline **

  9. Given a node from a DOM tree, find the node in the same position from an identical DOM tree.

  10. Callendar challenge (2015)

  11. Given a grid of characters output a decoded message. The message for the following would be IROCKA. (diagonally down right and diagonally up right if you can’t go further)

1
2
3
I B C A L K A
D R F C A E A
G H O E L A D
  1. Javascript asyn concepts: event bubbling, debounce (its variant)

  2. CSS position: relative, static. Difference inside out

  3. (debounce) If you were building a search tool and wanted search results to pop up as you typed, but the server call was taxing. Write a function that gets called on every key down but calls the server when the user stops typing for 400ms.

    • How many times would addEventListener('scroll', handleScroll); run as the user looks at their News Feed? And what would be user experience if the handleScroll function takes 100ms to execute.
    • How could you implement debouncing? Say you wanted the handleScroll function to be called only after 200ms has passed between scroll events.
  4. Array reduce function

  5. Design a class with some member functions that requries JS concepts such as closure and callbacks.

  6. Time complexity to traverse the balanced binary tree.

  7. Implement a simple store class with set(Node, value), get(Node), and has(Node) methods, which store a given Nodes with corresponding values. **

  8. You are given an array of N elements in the form “property1: value1; property2: value2;…;propertyX: valueX;” for some some N and any X. There is also another array of M elements of the form “property: value”. You are supposed to write an algorithm to remove every element in the N length array that has a “property: value” pair in the M length array.

  9. What’s structure of DOM. What’s the depth of the balanced DOM tree with n nodes>

https://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=191090&highlight=fb%2B%C7%B0%B6%CB

  1. function animateRight(element, duration, distance), smoothly move element in the duration.