Skip to content

Instantly share code, notes, and snippets.

// 实现一个浏览器的前进、后退与前进网址的功能
current -> a
backup -> c, b
class Navigator {
@brandonweis
brandonweis / reset.css
Created June 21, 2022 20:52 — forked from EllyLoel/reset.css
CSS Reset
/*
Made by Elly Loel - https://ellyloel.com/
With inspiration from:
- Josh W Comeau - https://courses.joshwcomeau.com/css-for-js/treasure-trove/010-global-styles/
- Andy Bell - https://piccalil.li/blog/a-modern-css-reset/
- Adam Argyle - https://unpkg.com/open-props@1.3.16/normalize.min.css / https://codepen.io/argyleink/pen/KKvRORE
Notes:
- `:where()` is used to lower specificity for easy overriding.
*/
const error_handler = (property) => {
throw new Error(`Error: ${property} is required!`);
}
const monitor_roads = (car=error_handler('Car'), speed=error_handler('Speed')) => {
console.log(`Your ${car} was going at a speed of ${speed}mph.`);
}
monitor_roads('Ferrari', 240); // Your Ferrari was going at a speed of 240mph.
monitor_roads('Ferrari'); // Error: Speed is required!
/*
# Coding exercise
Implement the array reduce function (including tests)
var arr1 = [1,2,3,4];
arr1.reduce((acc, value) => acc + value, 0); // 10
Extension: avoid using loops (use recursion)
*/
/*
You have 2 different applications running in the webpage, one with the cart count and another is a list of products. When you add click on "Add to cart" in the project, the cart count needs to get updated.
Implement a eventBus / pub/Sub solution to allow communication between the two applications.
Write tests to cover common use cases.
*/
const EventBus = () => {
const listeners = {}
// promise error handling
function fetchAndUpdatePosts() {
fetchPosts()
.then((posts) => {
updatePosts(posts)
.catch((err) => {
console.log('error in updating posts');
});
})
.catch(() => {
const caughtFunction = () => {
try {
blarg;
} catch (e) {
throw new Error('Ghaaa!');
}
}
const throwingFunction = () => {
throw new Error('Ghaaa!');
/*
Implement the array flat function.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
Creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.
var arr1 = [1, 2, [3, 4]];
arr1.flat(); // [1, 2, 3, 4]
var arr2 = [1, 2, [3, 4, [5, 6]]];
arr2.flat(); // [1, 2, 3, 4, [5, 6]]
export const cancellablePromise = promise => {
let isCanceled = false;
const wrappedPromise = new Promise((resolve, reject) => {
promise.then(
value => (isCanceled ? reject({ isCanceled, value }) : resolve(value)),
error => reject({ isCanceled, error }),
);
});
import React, { PropTypes } from 'react';
const Repeater = function(props, context) {
const count = props.count || 1;
const temp = [];
for (let i = 0; i < count; i++) {
temp.push(props.children);
}
console.log(temp);