Skip to content

Instantly share code, notes, and snippets.

@rootTheLure
Last active September 1, 2023 11:52
Show Gist options
  • Save rootTheLure/78bd89b50b9b692bfb53b2f3b7ce3080 to your computer and use it in GitHub Desktop.
Save rootTheLure/78bd89b50b9b692bfb53b2f3b7ce3080 to your computer and use it in GitHub Desktop.
// 1) Simple cache class with limited size
const c = new Cache(3); // 3 is maximum length of cache
// add(key, value), key any type, value any type;
c.add('one', 1);
c.add('two', { a: 2 });
c.add('three', '3');
c.get('one'); // -> 1
c.add('four', 4);
c.get('one'); // -> undefined
// 2) Restore gaps in the sequence
const restore = sequence([1, 5, 7, 9, 13]);
restore(); // -> [1, 3, 5, 7, 9, 11, 13]
// 3) Data service
const dataService = new DataService();
sortService.load(data);
sortService.top(n, field ); // n: number of items to return, field: name of field to sort by
// 4) loadService
// 5) sortService
// 6) Make this work
var a = (5).plus(3).minus(6);
// 7) You have a function rand5() that generates a random integer from 1 to 5.
// Use it to write a function rand7() that generates a random integer from 1 to 7.
// 8) We want to fill this array with numbers from the counter. We then want to halve
// each number in the array, and then filter only integer results.
const getNum = () => counter.step();
const halve = x => x / 2;
const isInt = x => Math.floor(x) === x;
// naive solution
bigArray
.map(getNum)
.map(halve)
.filter(isInt);
// performant solution
bigArray.reduce(newArr => {
const x = getNum();
const hlv = halve(x);
if (isInt(hlv)) {
newArr.push(hlv);
}
return newArr;
}, []);
// N) validate code
(function() {
var a = b = 5;
})();
console.log(b);
function test() {
console.log(a);
console.log(foo());
var a = 1;
function foo() {
return 2;
}
}
test();
//1. [-1, null, 2, 'five', 1].sort(); // => [1, 2, -1, null, 'five']
//2. sort all odd/even positions of array
//3. data struture add O(1), get O(1), remove 0(1), reset 0(1)
//4. point on surface
//5. App to generate pin code with designed complexity
//6. The longest english word, you can display on 7-segment display. /gkmqvwxz/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment