├── js_testScripts ├── assertAnd.txt ├── data.json ├── file.json ├── testScript.js ├── h.html ├── style.css ├── script.js ├── mycalb.html ├── XMLHttprequest_01.js ├── myLib.js ├── tryCatch_01.js ├── bindingThis.js ├── tree.js ├── buttons.html ├── ArrayFromEntries.js ├── index.html ├── symbol.js ├── thisStuff.js ├── classes_Objects.js ├── fireship.js ├── collections.js ├── generator.js ├── debug.log ├── hashMap_01.js ├── iteration.js ├── hashMap_02.js └── asyncAwait.js ├── HTML_CSS_01.jpg ├── Javascript_01.jpg ├── algorithms_01.jpg ├── Javascript_Responsive_01.jpg ├── Javascript_TipsAndTricks.jpg ├── Javascript_cryptography_01.jpg └── README.md /js_testScripts/assertAnd.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /js_testScripts/data.json: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /js_testScripts/file.json: -------------------------------------------------------------------------------- 1 | {"bla" : "blub"} -------------------------------------------------------------------------------- /js_testScripts/testScript.js: -------------------------------------------------------------------------------- 1 | console.log('testScritp Loaded') -------------------------------------------------------------------------------- /HTML_CSS_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mechaniac/Map-of-Javascript/HEAD/HTML_CSS_01.jpg -------------------------------------------------------------------------------- /Javascript_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mechaniac/Map-of-Javascript/HEAD/Javascript_01.jpg -------------------------------------------------------------------------------- /algorithms_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mechaniac/Map-of-Javascript/HEAD/algorithms_01.jpg -------------------------------------------------------------------------------- /js_testScripts/h.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | bla bla blub 4 | 5 | -------------------------------------------------------------------------------- /Javascript_Responsive_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mechaniac/Map-of-Javascript/HEAD/Javascript_Responsive_01.jpg -------------------------------------------------------------------------------- /Javascript_TipsAndTricks.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mechaniac/Map-of-Javascript/HEAD/Javascript_TipsAndTricks.jpg -------------------------------------------------------------------------------- /Javascript_cryptography_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mechaniac/Map-of-Javascript/HEAD/Javascript_cryptography_01.jpg -------------------------------------------------------------------------------- /js_testScripts/style.css: -------------------------------------------------------------------------------- 1 | 2 | 3 | #results li.pass { color: green; } 4 | #results li.fail { color: red; } 5 | -------------------------------------------------------------------------------- /js_testScripts/script.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | const l = console.log 3 | 4 | 5 | var x = 12; 6 | l(Number.isNaN(x)) 7 | 8 | 9 | 10 | l(x.toExponential(3)) 11 | 12 | function f(x){ 13 | return x*2 14 | } 15 | 16 | l(f.toString()) 17 | 18 | var a = [1,2,3] 19 | 20 | l(a.find(x => x<3)) -------------------------------------------------------------------------------- /js_testScripts/mycalb.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Document 7 | 8 | 9 |
my callback success
10 | 11 | -------------------------------------------------------------------------------- /js_testScripts/XMLHttprequest_01.js: -------------------------------------------------------------------------------- 1 | var r = new XMLHttpRequest() 2 | r.onreadystatechange = f; 3 | r.open('GET', 'file.json') 4 | r.send() 5 | 6 | 7 | 8 | function f(){ 9 | if(r.readyState === XMLHttpRequest.DONE){ 10 | if(r.status === 200){ 11 | console.log(r) 12 | 13 | } else{ 14 | console.log('xml errr') 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /js_testScripts/myLib.js: -------------------------------------------------------------------------------- 1 | function assert(value, desc) { 2 | var li = document.createElement("li"); 3 | li.className = value ? "pass" : "fail"; 4 | li.appendChild(document.createTextNode(desc)); 5 | document.getElementById("results").appendChild(li); 6 | } 7 | window.onload = function() { 8 | assert(true, "The test suite is running."); 9 | assert(false, "Fail!"); 10 | }; 11 | 12 | function report(text) { assert(true, text); } -------------------------------------------------------------------------------- /js_testScripts/tryCatch_01.js: -------------------------------------------------------------------------------- 1 | //SERVERSIDE 2 | 3 | 4 | 5 | let a = ['a','b','c'] 6 | 7 | function takesCb(p , callback) { 8 | 9 | 10 | try{ 11 | if(a[p]){ 12 | callback(undefined, a[p]) 13 | } else{ 14 | throw 'error' 15 | } 16 | 17 | } catch(e) { 18 | callback(e,undefined) 19 | } finally{ 20 | l('finished') 21 | } 22 | 23 | } 24 | 25 | 26 | 27 | // CLIENTSIDE 28 | 29 | takesCb(1,(err, c)=>{ 30 | if(err){ 31 | l(err); 32 | return; 33 | } 34 | l(c) 35 | }) -------------------------------------------------------------------------------- /js_testScripts/bindingThis.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const e = () => l(this) 4 | 5 | var l = console.log; 6 | 7 | const o = { 8 | a: 1, 9 | b () { 10 | l(this) 11 | }, 12 | c : () => {l(this)}, 13 | d: d, 14 | e: e 15 | } 16 | 17 | function Mo(p){ 18 | this.a = p 19 | this.b = function(){l(this)} 20 | this.c =()=> l(this) 21 | this.d = d 22 | this.e = e 23 | } 24 | 25 | function d(){ 26 | l(this) 27 | } 28 | 29 | const mo = new Mo(12) 30 | 31 | l('o: --------------------') 32 | 33 | o.b() 34 | o.c() 35 | o.d() 36 | o.e() 37 | 38 | //const mo2 = new Mo(33) 39 | //mo2.b() 40 | 41 | l('mo: --------------------') 42 | 43 | mo.b() 44 | mo.c() 45 | mo.d() 46 | mo.e() 47 | 48 | l('MO :--------------------') 49 | 50 | 51 | 52 | d() 53 | e() 54 | 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # map of javascript 2 | 3 | ![alt text](https://github.com/mechaniac/javaScript_cheatSheet/blob/main/Javascript_01.jpg?raw=true) 4 | 5 | data structures and alogrithms 6 | ![alt text](https://github.com/mechaniac/javaScript_cheatSheet/blob/main/algorithms_01.jpg?raw=true) 7 | 8 | HTML and CSS and DOM interaction 9 | ![alt text](https://github.com/mechaniac/javaScript_cheatSheet/blob/main/HTML_CSS_01.jpg?raw=true) 10 | 11 | Cryptography 12 | ![alt text](https://github.com/mechaniac/javaScript_cheatSheet/blob/main/Javascript_cryptography_01.jpg?raw=true) 13 | 14 | responsive 15 | ![alt text](https://github.com/mechaniac/javaScript_cheatSheet/blob/main/Javascript_Responsive_01.jpg?raw=true) 16 | 17 | ![alt text](https://github.com/mechaniac/javaScript_cheatSheet/blob/main/Javascript_TipsAndTricks.jpg?raw=true) 18 | -------------------------------------------------------------------------------- /js_testScripts/tree.js: -------------------------------------------------------------------------------- 1 | const log = console.log 2 | 3 | 4 | const data = [ 5 | { id: 56, parentId: 62 }, 6 | { id: 81, parentId: 80 }, 7 | { id: 74, parentId: null }, 8 | { id: 76, parentId: 80 }, 9 | { id: 63, parentId: 62 }, 10 | { id: 80, parentId: 86 }, 11 | { id: 87, parentId: 86 }, 12 | { id: 62, parentId: 74 }, 13 | { id: 86, parentId: 74 }, 14 | ]; 15 | 16 | const idMapping = data.reduce((acc, el, i) => { 17 | acc[el.id] = i; 18 | return acc; 19 | }, {}); 20 | 21 | log(idMapping) 22 | 23 | let root; 24 | data.forEach(el => { 25 | 26 | if (el.parentId === null) { 27 | root = el; 28 | return; 29 | } 30 | const parentEl = data[idMapping[el.parentId]]; 31 | parentEl.children = [...(parentEl.children || []), el]; 32 | log(`parentEl= ${parentEl}`) 33 | }); 34 | 35 | log(root) 36 | log(idMapping) -------------------------------------------------------------------------------- /js_testScripts/buttons.html: -------------------------------------------------------------------------------- 1 | 2 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /js_testScripts/ArrayFromEntries.js: -------------------------------------------------------------------------------- 1 | 2 | var a = new Array('a','b','b') 3 | 4 | l(a) 5 | 6 | var m = new Map([[1,'a'],[2,'b'],[3,'b']]) 7 | // v 8 | l(m) 9 | 10 | var s = new Set(['a','b','b']) 11 | 12 | l(s) 13 | 14 | const sm = Object.fromEntries(m) 15 | l(sm) 16 | const sa = Object.fromEntries(a.entries()) 17 | l('arrrr') 18 | l(sa) 19 | // const ss = Object.fromEntries(s) 20 | 21 | 22 | const arr = [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]; 23 | const obj = Object.fromEntries(arr); 24 | console.log(obj); // { 0: "a", 1: "b", 2: "c" } 25 | 26 | 27 | // l(sa) 28 | // l(sm) 29 | // l(ss) 30 | 31 | const o ={a:'a',b: 'b'} 32 | l('emn') 33 | var oaoa = Object.entries(o) 34 | l(typeof(a)) 35 | l(Array.isArray(oaoa)) 36 | 37 | l(o) 38 | l(oaoa) 39 | 40 | const {a:myA, b:myB} = o; 41 | 42 | l(myA) 43 | 44 | const s1 = Symbol('a') 45 | const s2 = Symbol('a') 46 | 47 | l(Symbol('a') === Symbol('a')) 48 | 49 | 50 | 51 | const aa = Array.from(Array(5).keys(), x=> x*2) 52 | l(aa) 53 | 54 | 55 | var ax = { 56 | 'prop': 'val', 57 | prop2: 'val2', 58 | 'nested':{'a':123} 59 | } 60 | 61 | l(Object.entries(ax)) -------------------------------------------------------------------------------- /js_testScripts/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 |
20 | 21 |
22 |
23 |
24 | 25 |
26 | 27 | 28 | 50 | hello 51 | 52 | 53 | -------------------------------------------------------------------------------- /js_testScripts/symbol.js: -------------------------------------------------------------------------------- 1 | 2 | // //------------ 3 | // var o = { 4 | // "prop":"val", 5 | // prop2: "val2", 6 | // nested:{ 7 | // a:123 8 | // } 9 | // } 10 | 11 | // var c = Object.create(o) 12 | 13 | // l(Object.keys(o)) 14 | // l(Object.keys(c)) 15 | 16 | 17 | //---------------symbol----------- 18 | 19 | // var s = Symbol('s') 20 | // l(s) 21 | // l([s]) 22 | 23 | 24 | const string = 'whatever' 25 | const normalObject = { 26 | [string]: 'something else' 27 | } 28 | 29 | const s = Symbol('a') 30 | const s2 = Symbol('b') 31 | const o = { 32 | s: 'b' 33 | } 34 | 35 | 36 | const o2 = { 37 | [s]: 'b', 38 | x:'x', 39 | [s2]: 'b2' 40 | } 41 | 42 | l(o2[s2]) 43 | 44 | // l(o) 45 | // l(o2) 46 | // l(o2[st]) 47 | 48 | // for(var x in o) {l(x)} 49 | 50 | for(var x in o2) {l(x)} 51 | 52 | 53 | var a = [] 54 | // l(a[Symbol.iterator] === undefined) 55 | // l(o2[Symbol.iterator] === undefined) 56 | 57 | 58 | 59 | l(Object.getOwnPropertySymbols(o2)) 60 | l(o.a) 61 | 62 | let ox = {a:'b'} 63 | let oy = {a:'b'} 64 | 65 | l(ox === oy) 66 | 67 | var bla = 12 68 | 69 | l(undefined ?? bla) 70 | 71 | 72 | var a = ['a','b','c'] 73 | 74 | for(var x of a.values()){ 75 | l(x) 76 | } -------------------------------------------------------------------------------- /js_testScripts/thisStuff.js: -------------------------------------------------------------------------------- 1 | function forEach(l, cb) { 2 | for (var n = 0; n < l.length; n++) { 3 | cb.call(l[n], n); 4 | } 5 | } 6 | 7 | var weapons = [ { x: 'a' }, 8 | { x: 'b' }, 9 | { x:'c' }]; 10 | 11 | forEach(weapons, function(index) { console.log( this === weapons[index] )}); 12 | 13 | //////////////////////////////// 14 | 15 | var mO={ a: 1} 16 | 17 | function f(o,cb){ 18 | 19 | cb.call(o) 20 | } 21 | 22 | f(mo,function(){console.log(this)}) 23 | 24 | o2 = { 25 | a:12, 26 | f: (mo,()=> console.log(this)), 27 | f2 (){ console.log(this)} 28 | } 29 | 30 | 31 | o2.f() 32 | o2.f2() 33 | 34 | 35 | let me = { 36 | name: "Ashutosh Verma", 37 | thisInArrow:() => { 38 | console.log(this); // no 'this' binding here 39 | }, 40 | thisInRegular(){ 41 | console.log(this); // 'this' binding works here 42 | } 43 | }; 44 | me.thisInArrow(); 45 | me.thisInRegular(); 46 | 47 | 48 | function MyObject(p){ 49 | this.n=p 50 | this.f1 = ()=> console.log(this) 51 | this.f2 = function(){console.log(this)} 52 | } 53 | 54 | var o = new MyObject('a') 55 | 56 | o.f1() 57 | o.f2() 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /js_testScripts/classes_Objects.js: -------------------------------------------------------------------------------- 1 | 2 | function F(p){ 3 | this.x = p; 4 | } 5 | 6 | var f = new F(1) 7 | 8 | class C{ 9 | static MY_C ='x' 10 | #y = 12; 11 | constructor(x){ 12 | this.x = x; 13 | } 14 | sum(){ 15 | return this.x + this.#y; 16 | } 17 | } 18 | 19 | const c = new C(2) 20 | 21 | l(C.prototype.__proto__ === Object.prototype) 22 | l('c:') 23 | l(c.__proto__ === C.prototype) 24 | 25 | var o = { 26 | x: 2 27 | } 28 | 29 | l(o) 30 | 31 | l(f) 32 | l('f') 33 | l(f.__proto__ === F.prototype) 34 | l(Object.getPrototypeOf(f)) 35 | 36 | l(F) 37 | 38 | l(o.__proto__ === Object.prototype) 39 | 40 | l(F.prototype.__proto__ === Object.prototype) 41 | l(F.__proto__ === Function.prototype) 42 | l(Function.prototype === Function.__proto__) 43 | l(Object.__proto__ === Function.prototype) 44 | 45 | l(C.prototype.constructor === C) 46 | l(c.__proto__ === C.prototype) 47 | 48 | 49 | 50 | //l(c.#y) // => Syntax Error 51 | l(c.sum()) 52 | 53 | C.MY_C = 'b' 54 | l(C.MY_C) 55 | 56 | l("ab".localeCompare("a")) 57 | 58 | class B { 59 | #a = 3; 60 | get a(){ 61 | return this.#a 62 | } 63 | set a(a){ 64 | this.#a = a 65 | } 66 | } 67 | 68 | const b = new B() 69 | 70 | l(b.a) 71 | b.a = 12 72 | l(b.a) 73 | 74 | class D extends B{} 75 | 76 | const d = new D() 77 | 78 | d.a = 13 79 | l(d.a) -------------------------------------------------------------------------------- /js_testScripts/fireship.js: -------------------------------------------------------------------------------- 1 | // l('sync') 2 | 3 | // setTimeout(_=> l('sto')) 4 | 5 | // Promise.resolve().then(_=> l('p')) 6 | 7 | // l('sync') 8 | 9 | // //PROMISES 10 | 11 | // const p = fetch(('https://jsonplaceholder.typicode.com/todos/1')) 12 | 13 | // p.then(res => res.json()) 14 | // .then() 15 | // .then(user => l(user.title)) 16 | // .catch(err => l(err)) 17 | 18 | 19 | let tick = Date.now(); 20 | const log = (v) => { l(`${v} \t Elapsed: ${Date.now() - tick}`); tick = Date.now()} 21 | 22 | const codeBlocker = ()=> { 23 | let i = 0; 24 | while(i< 100000000){i++} 25 | 26 | return '1 Billion Loops' 27 | } 28 | 29 | const codeBlockerPromiseBad = () => { 30 | return new Promise((resolve, reject)=>{ 31 | let i = 0; 32 | while(i< 100000000){i++} 33 | 34 | resolve('1 Billion Loops') 35 | }) 36 | } 37 | 38 | const codeBlockerPromiseGood = () => { 39 | 40 | return Promise.resolve().then( v=>{ 41 | let i = 0; 42 | while(i < 1000000000){i++} 43 | return '1Billon Loops' 44 | }) 45 | } 46 | 47 | // log('codeBlocker Start') 48 | // log(codeBlocker()) 49 | // log('End') 50 | 51 | 52 | // log('codeBlockerPromiseBad Start') 53 | // codeBlockerPromiseBad().then(log) 54 | // log('End') 55 | 56 | 57 | 58 | log('codeBlockerPromiseGood Start') 59 | codeBlockerPromiseGood().then(log) 60 | 61 | 62 | l(Promise.prototype) -------------------------------------------------------------------------------- /js_testScripts/collections.js: -------------------------------------------------------------------------------- 1 | 2 | var m = new Map([[1,'a'],[2,'b'],[3,'c']]) 3 | 4 | l(m.get(1)) 5 | l(m.size) 6 | m.set(4,'d') 7 | l(m) 8 | 9 | 10 | var a = new Array('a', 'b', 'c') 11 | l(a) 12 | 13 | var o = {'0':'a', 14 | '1':'b', 15 | '2':'c'} 16 | 17 | l(o) 18 | 19 | //m.clear() 20 | l(m) 21 | 22 | let myO = {x: 'y'} 23 | 24 | m.set(myO, 'e') 25 | 26 | l(m) 27 | 28 | m.delete(2) 29 | l(m) 30 | 31 | l('keys: ') 32 | 33 | for(let k of m.keys()){ 34 | l(k) 35 | } 36 | 37 | l('values: ') 38 | 39 | 40 | for(let k of m.values()){ 41 | l(k) 42 | } 43 | 44 | l('entriess: ') 45 | 46 | 47 | for(let k of m.entries()){ 48 | l(k) 49 | } 50 | 51 | 52 | l('forEach : ') 53 | 54 | m.forEach((v,k,m) =>{ 55 | l(`v: ${v}, k: ${k}, m: ${m}`) 56 | }) 57 | 58 | //------------------------------------------------------- 59 | 60 | let aa = ['a', 'b', 'c'] 61 | 62 | let mm = new Map(Object.entries(aa)) 63 | 64 | l(aa) 65 | l(mm) 66 | 67 | let ooo = Object.fromEntries(mm) 68 | 69 | l(ooo) 70 | 71 | //++++++++++++++++++++++++++++++++ 72 | 73 | var s = new Set(['a', 'b', 'c']) 74 | 75 | l(s) 76 | 77 | l('entries: ') 78 | for(var x of s.entries()){ 79 | l(x) 80 | } 81 | 82 | l('keys: ') 83 | for(var x of s.keys()){ 84 | l(x) 85 | } 86 | 87 | l('values: ') 88 | for(var x of s.values()){ 89 | l(x) 90 | } 91 | 92 | 93 | l('s: ') 94 | for(var x of s){ 95 | l(x) 96 | } 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /js_testScripts/generator.js: -------------------------------------------------------------------------------- 1 | function* myGenerator(){ 2 | l('a') 3 | 4 | yield 12; 5 | 6 | l('b') 7 | 8 | yield 22; 9 | } 10 | 11 | let g = myGenerator() 12 | 13 | 14 | 15 | //l(g) 16 | 17 | 18 | 19 | // l( g.next()) 20 | // l( g.next()) 21 | // l( g.next()) 22 | 23 | 24 | 25 | 26 | let g2 = myGenerator() 27 | 28 | // for(const x of g2){ 29 | // x.value; 30 | // } 31 | function objectify(key, value){ 32 | let o = {} 33 | o[key] = value 34 | return o 35 | } 36 | 37 | function objectify2(key,value){ 38 | return{ 39 | [key]: value 40 | } 41 | } 42 | 43 | let myO = objectify2('name', 'theC') 44 | //l(myO) 45 | 46 | 47 | const o = { 48 | oi:{ 49 | a:['a1'], 50 | b:['b1','b2'], 51 | c:['c1','c2','c3'], 52 | }, 53 | 54 | [Symbol.iterator]() { 55 | const ra = Object.values(this.oi); 56 | 57 | let ia = 0; 58 | let i = 0; 59 | 60 | return { 61 | next() { 62 | 63 | if(i>=ra[ia].length) { 64 | ia++ 65 | i = 0 66 | } 67 | 68 | if(ia 1) { this.collisions++; } 27 | } else { 28 | this.a[i] = [{ key: k, value: v }]; 29 | } 30 | return this; 31 | } 32 | 33 | /** 34 | * Gets the value out of the hash map 35 | * @param {any} k 36 | */ 37 | get(k) { 38 | const i = this.getIndex(k); 39 | for (let a_i = 0; a_i < this.a[i].length; a_i++) { 40 | const e = this.a[i][a_i]; 41 | if (e.key === k) { 42 | return e.value; 43 | } 44 | } 45 | } 46 | 47 | /** 48 | * This hash function returns the sum of the char codes. 49 | * Limitation same characters return the same code regardless of the order 50 | * @param {any} k 51 | */ 52 | hash(k) { 53 | let h = 0; 54 | const s = `${k}${typeof k}`; 55 | 56 | for (let i = 0; i < s.length; i++) { 57 | const c = s.charCodeAt(i); 58 | //log(`c inside = ${c}`) 59 | h += c << (i * 8); 60 | //log(`h inside = ${h}`) 61 | } 62 | //log(`h = ${h}`) 63 | return h; 64 | } 65 | 66 | /** 67 | * Get the array index after applying the hash funtion to the given key 68 | * @param {any} k 69 | */ 70 | getIndex(k) { 71 | const h = this.hash(k); 72 | return h % this.a.length; 73 | } 74 | } 75 | 76 | 77 | let h = new MyHashMap() 78 | 79 | log(h) 80 | h.set('a',12) 81 | h.set('b',22) 82 | log(h) 83 | 84 | 85 | let mm = new Map() 86 | 87 | mm.set('a',12) 88 | mm.set('b',22) 89 | 90 | log('mm: ') 91 | log(mm) 92 | 93 | //module.exports = MyHashMap; 94 | -------------------------------------------------------------------------------- /js_testScripts/iteration.js: -------------------------------------------------------------------------------- 1 | const myFavouriteAuthors = { 2 | allAuthors: { 3 | fiction: [ 4 | 'Agatha Christie', 5 | 'J. K. Rowling', 6 | 'Dr. Seuss' 7 | ], 8 | scienceFiction: [ 9 | 'Neal Stephenson', 10 | 'Arthur Clarke', 11 | 'Isaac Asimov', 12 | 'Robert Heinlein' 13 | ], 14 | fantasy: [ 15 | 'J. R. R. Tolkien', 16 | 'J. K. Rowling', 17 | 'Terry Pratchett' 18 | ], 19 | }, 20 | [Symbol.iterator]() { 21 | // Get all the authors in an array 22 | const genres = Object.values(this.allAuthors); 23 | 24 | // Store the current genre and author index 25 | let currentAuthorIndex = 0; 26 | let currentGenreIndex = 0; 27 | 28 | return { 29 | // Implementation of next() 30 | next() { 31 | // authors according to current genre index 32 | const authors = genres[currentGenreIndex]; 33 | 34 | // doNotHaveMoreAuthors is true when the authors array is exhausted. 35 | // That is, all items are consumed. 36 | const doNothaveMoreAuthors = !(currentAuthorIndex < authors.length); 37 | if (doNothaveMoreAuthors) { 38 | // When that happens, we move the genre index to the next genre 39 | currentGenreIndex++; 40 | // and reset the author index to 0 again to get new set of authors 41 | currentAuthorIndex = 0; 42 | } 43 | 44 | // if all genres are over, then we need tell the iterator that we 45 | // can not give more values. 46 | const doNotHaveMoreGenres = !(currentGenreIndex < genres.length); 47 | if (doNotHaveMoreGenres) { 48 | // Hence, we return done as true. 49 | return { 50 | value: undefined, 51 | done: true 52 | }; 53 | } 54 | 55 | // if everything is correct, return the author from the 56 | // current genre and incerement the currentAuthorindex 57 | // so next time, the next author can be returned. 58 | return { 59 | value: genres[currentGenreIndex][currentAuthorIndex++], 60 | done: false 61 | } 62 | } 63 | }; 64 | } 65 | }; 66 | 67 | for (const author of myFavouriteAuthors) { 68 | console.log(author); 69 | } 70 | 71 | console.log(...myFavouriteAuthors) -------------------------------------------------------------------------------- /js_testScripts/hashMap_02.js: -------------------------------------------------------------------------------- 1 | const log = console.log 2 | 3 | class HashMap { 4 | constructor(l = 5, f = 1) { 5 | this.a = new Array(l); 6 | this.f = f; 7 | this.s = 0; 8 | this.c = 0; 9 | this.keys = []; 10 | } 11 | 12 | hash(k) { 13 | let h = 0; 14 | const s = `${k}${typeof k}`; 15 | l(`s = ${s}`) 16 | for (let i = 0; i < s.length; i++) { 17 | let cC = s.charCodeAt(i); 18 | log(`cC = ${cC}, ${cC.toString(2)}`) 19 | cC = cC << (i * 8); 20 | log(`cC = ${cC}, ${cC.toString(2)}`) 21 | h += cC 22 | log(`h = ${h}`) 23 | } 24 | 25 | let hh = h % this.a.length; 26 | 27 | console.log(`hash: k=${k}, hh= ${hh}, h= ${h}`) 28 | return hh; 29 | } 30 | 31 | 32 | 33 | set(k, v) { 34 | let { h, i } = this._getIs(k); 35 | 36 | if (i === undefined) { 37 | // initialize array and save key/value 38 | i = this.keys.push({ d: k }) - 1; // keep track of the key index 39 | this.a[h] = this.a[h] || []; 40 | this.a[h].push({ k, v, kI: i }); 41 | this.s++; 42 | // Optional: keep count of collisions 43 | if (this.a[h].length > 1) { this.c++; } 44 | } else { 45 | // override existing value 46 | this.a[h][i].v = v; 47 | } 48 | 49 | // check if a rehash is due 50 | if (this.f > 0 && (this.s / this.a.length) > this.f) { 51 | //console.log(`Rehash:`) 52 | this.rehash(this.a.length * 2); 53 | 54 | } 55 | 56 | return this; 57 | } 58 | 59 | get(k) { 60 | const { h, i } = this._getIs(k); 61 | 62 | if (i === undefined) { 63 | return; 64 | } 65 | 66 | return this.a[h][i].v; 67 | } 68 | 69 | has(k) { 70 | return !!this.get(k); 71 | } 72 | 73 | _getIs(k) { 74 | const h = this.hash(k); 75 | const vs = this.a[h] || []; 76 | 77 | for (let i = 0; i < vs.length; i++) { 78 | const e = vs[i]; 79 | if (e.k === k) { 80 | //console.log(`getIs: bI: ${h}, i: ${i}`) 81 | return { h, i }; 82 | } 83 | } 84 | 85 | return { h }; 86 | } 87 | 88 | 89 | _getBI(k) { 90 | const h = this.hash(k); 91 | return h % this.a.length; 92 | } 93 | 94 | delete(k) { 95 | const { h, i, kI } = this._getIs(k); 96 | //log(`h = ${h}, i = ${i}, kI = ${kI}`) 97 | if (i === undefined) { 98 | return false; 99 | } 100 | 101 | this.a[h].splice(i, 1); 102 | delete this.keys[kI]; 103 | this.s--; 104 | 105 | return true; 106 | } 107 | 108 | rehash(l) { 109 | const m = new HashMap(l); 110 | 111 | this.keys.forEach(k => { 112 | if (k) { 113 | m.set(k.d, this.get(k.d)); 114 | } 115 | }); 116 | 117 | // update bucket 118 | this.a = m.a; 119 | this.c = m.c; 120 | // Optional: both `keys` has the same content except that the new one doesn't have empty spaces from deletions 121 | this.keys = m.keys; 122 | } 123 | 124 | _getF() { 125 | return this.s / this.a.length; 126 | } 127 | } 128 | 129 | 130 | var m = new HashMap() 131 | 132 | m.set('ab', 12) 133 | m.set('ae', 22) 134 | m.set('at', 32) 135 | //m.delete('ab') 136 | // m.set('d',42) 137 | // m.set('e',42) 138 | // m.set('f',42) 139 | // m.set('g',42) 140 | // m.set('h',42) 141 | // m.set('i',42) 142 | // m.set('j',42) 143 | 144 | log(`----------------------`) 145 | 146 | log(m) 147 | 148 | log(m.get('ab')) 149 | //log(m.get('b')) 150 | -------------------------------------------------------------------------------- /js_testScripts/asyncAwait.js: -------------------------------------------------------------------------------- 1 | 2 | 3 | l('ASYNC AWAIT') 4 | 5 | var data =['a','b','c'] 6 | 7 | // function hO(i, cb){ 8 | // if(data[i]){ 9 | // cb(undefined, data[i]) 10 | // } else{ 11 | // cb('err') 12 | // } 13 | // } 14 | // hO(0, (e,d)=> {if(e){l(e); return} l(d)}) 15 | 16 | 17 | // function myP(i){ 18 | 19 | // return new Promise(function(resolve, reject){ 20 | 21 | // if(data[i]){ 22 | // resolve(data[i]); 23 | // } else{ 24 | // reject('err'); 25 | // } 26 | // }) 27 | 28 | // } 29 | // myP(2).then( 30 | // (d)=> l(d), 31 | // (e)=> l(e) 32 | // ) 33 | 34 | 35 | // async function mA(i){ 36 | // let p = new Promise(function(resolve, reject){ 37 | // if(data[i]){ 38 | // resolve(data[i]) 39 | // }else{ 40 | // reject('err') 41 | // } 42 | // }) 43 | // await p 44 | // } 45 | l('start') 46 | 47 | var b = false 48 | 49 | var p = new Promise((res, rej) =>{ 50 | this.__proto__.then = ()=> {l('fromThen')} 51 | setTimeout(function(){ 52 | res({ 53 | message: "fully filly", 54 | code: "wohoo" 55 | }) 56 | }, 1*1000) 57 | 58 | }) 59 | 60 | 61 | l(p) 62 | 63 | 64 | //-------------------------------------------------- 65 | 66 | 67 | function loadScript(src, callback) { 68 | // creates a