Array (Queue, Stack, Tree)
1. forEach()
and map()
What's the difference between forEach()
and map()
?
- Return value
- ?
- ?
What does
const arr = [0, 0, 0, 0, 1, 0, 0, 0];
arr.forEach(function(val, index) {
if (val === 1) break;
else arr[index] = 3;
})
console.log(arr); // ?
Answer is Uncaught SyntaxError: Illegal break statement
. There's no built-in ability to break
in forEach
.
2. every()
and some()
What's the difference between every()
and some()
?
3. slice()
and splice()
There's no splice()
for strings.
Array.prototype.slice; // exist
String.prototype.slice; // exist
Array.prototype.splice; // exist
String.prototype.splice; // undefined, use subString() or slice()
Use splice to insert items into array:
//
3. Implement Stack using Array
How to use array as stack?
const stack = []; // stack: []
stack.pop() // undefined
stack.push(1) // stack: [1]
stack.push(3) // stack: [1, 3]
stack.pop() // stack: [1], 3 is returned
stack[stack.length-1]; // peak()
4. Implement Queue using Array
How to use array as queue?
const queue = []; // queue: []
queue.push(2); // queue: [2]
queue.push(4); // queue: [2, 4]
const i = queue.shift() // queue: [4], 2 is returned
5. Sort Array
What will the following return?
[1,2,11,3].sort();
Answer is: [1, 11, 2, 3]
.
A little surprising right? Reason is sort(comparator)
's default comparator treats array items as string
.
Fix:
[1, 2, 11, 3].sort((a, b) => a - b);
// [1, 2, 3, 11]
More about Array.prototype.sort()
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
References:
- 9 JavaScript Tips You May Not Know http://codetunnel.io/9-javascript-tips-you-may-not-know/
- Interviewing a front-end developer http://blog.sourcing.io/interview-questions?utm_source=ourjs.com
- How to short circuit Array.forEach like calling break http://stackoverflow.com/questions/2641347/how-to-short-circuit-array-foreach-like-calling-break