⏱ 0:06est. 22 min
Practice
const data = {
designId: 1,
shapes: [
{shapeId: 'basic-shape', color: { r: 55, g: 40, b: 255 }, children: []},
{shapeId: 'duck', color: { r: 255, g: 255, b: 252 }, children: [
{shapeId: 'duck-bill', color: { r: 255, g: 255, b: 255 }, children: []},
{shapeId: 'duck-body', color: { r: 205, g: 255, b: 252 }, children: []},
{shapeId: 'duck-legs', color: { r: 100, g: 255, b: 252 }, children: []},
]},
{shapeId: 'zigzag-polygon', color: { r: 205, g: 255, b: 252 }, children: []},
{shapeId: 'fish', color: { r: 205, g: 255, b: 252 }, children: [
{shapeId: 'fish-eyes', color: { r: 255, g: 255, b: 255 }, children: []},
{shapeId: 'fish-fin', color: { r: 100, g: 66, b: 74 }, children: [
{shapeId: 'fish-fin-part-1', color: { r: 93, g: 54, b: 55 }, children: []},
{shapeId: 'fish-fin-part-2', color: { r: 33, g: 255, b: 255 }, children: []},
{shapeId: 'fish-fin-part-3', color: { r: 128, g: 53, b: 255 }, children: []},
]},
{shapeId: 'fish-tail', color: { r: 255, g: 5, b: 255 }, children: []},
]},
{shapeId: 'duck', color: { r: 255, g: 255, b: 252 }, children: [
{shapeId: 'duck-bill', color: { r: 255, g: 255, b: 255 }, children: []},
{shapeId: 'duck-body', color: { r: 205, g: 255, b: 252 }, children: []},
{shapeId: 'duck-legs', color: { r: 100, g: 255, b: 252 }, children: []},
]},
]
}
const fetch = (url) => {
const urlParts = url.split("/");
return new Promise((resolve, reject) => {
// setTimeout(reject({result: 'not done'}), 2000)
setTimeout(resolve({...data, designId:urlParts[urlParts.length-1]}), 3000)
})
}
async function helloWorld() {
const myResponse = await fetch("http://hello.world/1")
console.log(myResponse);
}
function fetchAllImages() {
let responsePromises = [];
for(let i=0; i<10; i++) {
responsePromises.push(fetch(`http://hello.world/${i+1}`));
}
// console.log(responsePromises);
return responsePromises;
}
// helloWorld();
function getAvgsOfResponse(response) {
let avgR = 0, avgG = 0, avgB = 0;
response.forEach((designer) => {
designer.shapes.forEach(element => {
// console.log('element', element)
avgR += element.color.r
avgG += element.color.g
avgB += element.color.b
})
designer.avgR = avgR/designer.shapes.length;
designer.avgG = avgG/designer.shapes.length;
designer.avgB = avgB/designer.shapes.length;
})
// console.log('getAvgsOfResponse', JSON.stringify(response, null, 2));
console.log('getAvgsOfResponse', JSON.stringify(response));
}
function getAllPromises() {
const responsePromises = fetchAllImages();
Promise.all(responsePromises).then((response) => {
// console.log(response);
return response;
}).then(getAvgsOfResponse).then(()=> {
console.log("DONE");
}).catch((e) => {
console.error("Fetch error", e);
})
}
// getAllPromises();
function mergeIntervals(arr) {
const sortedIntervals = arr.sort((a, b)=> a[0]-b[0]);
const result = [];
for(let i=0; i< sortedIntervals.length; i++) {
if (sortedIntervals[i+1] && sortedIntervals[i+1][0]<=sortedIntervals[i][1]) {
sortedIntervals[i+1][0] = Math.min(sortedIntervals[i+1][0], sortedIntervals[i][0]);
sortedIntervals[i+1][1] = Math.max(sortedIntervals[i+1][1], sortedIntervals[i][1]);
} else {
result.push(sortedIntervals[i]);
}
}
console.log(result)
}
let arr = [
[3, 5], // min 3, max 5
[1, 3],
[7, 9]
]; // output [[1,5], [7,9]];
// arr = [[7, 10], [1, 5], [12, 13], [2, 6], [7, 11]]
mergeIntervals(arr);
function cat(name) {
return {
fullName: function() {
return `your name ${name}`
}
}
}
let a = new cat("ok");
console.log(a.fullName());
let b = new cat("not-ok");
console.log(b.fullName());
console.log(a.fullName());
function sum(a) {
return function (b) {
if(b) {
return sum(a+b);
}
return a
}
}
// let sumWith2 = sum(2);
// console.log(sumWith2(3));
// console.log(sum(2)(2)(3)());
// const debounce = (fn, time) => {
// let timeout = null
// return function () {
// clearTimeout(timeout);
// timeout = setTimeout(() => {
// fn.apply(this, arguments);
// }, time);
// }
// }
const debounce = (fn, time) => {
let timer = null
return function () {
clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this, arguments);
}, time)
}
}
function sumWithDebounce(x) {
console.log(x+2)
}
const doSum = sumWithDebounce(1);
debounce(doSum, 200)
debounce(doSum, 200)
debounce(doSum, 200)
debounce(doSum, 200)
let flag = true;
const throttle = (fn, limit) => {
console.log('called', flag);
return function () {
if (flag) {
fn.apply(this, arguments)
flag = false;
setTimeout(() => {
flag = true;
console.log('changed');
}, limit)
}
}
}
function sumWithThrottle(x) {
console.log('sumWithThrottle', x+2);
}
const doSum1 = sumWithThrottle(500);
console.log('start')
throttle(doSum1, 1)
console.log('--1')
throttle(doSum1, 1)
console.log('--2')
throttle(doSum1, 1)
throttle(doSum1, 1)
console.log('--3')
throttle(doSum1, 1)
console.log('--4')
console.log('--2')
throttle(doSum1, 1)
console.log('--3')
throttle(doSum1, 1)
console.log('--4')
console.log('--2')
throttle(doSum1, 1)
console.log('--3')
throttle(doSum1, 1)
console.log('--4')
throttle(doSum1, 1)
console.log('--4')
console.log('--2')
throttle(doSum1, 1)
console.log('--3')
throttle(doSum1, 1)
console.log('--4')
console.log('--2')
throttle(doSum1, 1)
console.log('--3')
throttle(doSum1, 1)
console.log('--4')
throttle(doSum1, 1)
throttle(doSum1, 1)
throttle(doSum1, 1)
throttle(doSum1, 1)
throttle(doSum1, 1)
throttle(doSum1, 1)
throttle(doSum1, 1)
throttle(doSum1, 1)
throttle(doSum1, 1)
throttle(doSum1, 1)
throttle(doSum1, 1)
throttle(doSum1, 1)
throttle(doSum1, 1)
throttle(doSum1, 1)
throttle(doSum1, 1)
throttle(doSum1,0)
console.log('end')
// ----
// const request = require('request');
// const url = '';
// async function doCall () {
// return new Promise((resolve, reject) => {
// request(url, (response) => {
// resolve(response)
// })
// });
// }
// async function main() {
// const res = await doCall();
// console.log(res);
// }
// main()
function Person () {
return 2;
}
var p = Person();
var p1 = new Person();
console.log(p);
console.log(p1);
function flatten(array, result) {
if (array.length === 0) {
return result
}
let head = array[0];
let rest = array.slice(1);
if (Array.isArray(head)) {
return flatten(head.concat(rest), result);
}
result.push(head)
return flatten(rest, result)
}
console.log(flatten([1], []))
console.log(flatten([[1,2,3],[[4,5],6,[7,8,9]]], []))
function factorial(n) {
if (n) {
return n * factorial(n-1);
}
return 1;
}
console.log(factorial(6));
function fib(n) {
if (n > 1) {
return fib(n-1) + fib(n-2);
}
return n;
}
for(let i=0; i<10;i++) {
console.log(fib(i));
}
let input = [[1,3], [2,5], [6,9]];
function mergeIntervals(intervals) {
intervals.sort((a,b) => a[0]-b[0]);
let result = [];
for(let i=0; i<intervals.length;i++) {
if (i+1<=mergeIntervals.length && intervals[i+1][0] < intervals[i][1]) {
intervals[i+1][0] = Math.min(intervals[i+1][0], intervals[i][0])
intervals[i+1][1] = Math.max(intervals[i+1][1], intervals[i][1])
} else {
result.push(intervals[i]);
}
}
console.log(result);
}
mergeIntervals(input);
function debounceNew(fn, time) {
let timer = null;
return function () {
clearTimeout(timer);
timer = setTimeout(() => {
if (!timer) {
fn.apply(this, arguments);
}
}, time)
}
}
function sum (x) {
console.log(x+2)
}
let sumNew = sum(4);
debounceNew(sumNew, 100)
debounceNew(sumNew, 100)
debounceNew(sumNew, 100)
debounceNew(sumNew, 100)
debounceNew(sumNew, 100)
let flag = true
function throttleNew(fn, time) {
return function() {
if (flag) {
fn.apply(this, arguments);
flag = false
setTimeout(() => {
flag = true;
}, time)
}
}
}