CHALLENGE
function* idGenerator() {
let id = 1;
while (true) {
yield id++;
}
}
const gen = idGenerator();
const weakMap = new WeakMap();
const objs = [{}, {}, {}];
objs.forEach(obj => weakMap.set(obj, gen.next().value));
const result = objs.map(obj => weakMap.get(obj)).filter(id => id % 2 === 0);
console.log(result);
CHALLENGE
const weakMap = new WeakMap();
const obj = {};
(function() {
const obj1 = { name: 'inner' };
weakMap.set(obj1, 'inner value');
})();
const result = weakMap.get(obj);
console.log(result);
CHALLENGE
const weakMap = new WeakMap();
const obj = {};
const gen = (function* () {
yield 'value1';
yield 'value2';
})();
weakMap.set(obj, gen.next().value);
console.log(weakMap.get(obj));
console.log(gen.next().value);
CHALLENGE
function* evenNumbers() {
let num = 0;
while (true) {
yield num;
num += 2;
}
}
const gen = evenNumbers();
const evens = Array.from({ length: 4 }, () => gen.next().value).map(n => n + 1);
console.log(evens);
CHALLENGE
const weakMap = new WeakMap();
const gen = (function* () {
yield { key: 'value1' };
yield { key: 'value2' };
})();
const obj1 = gen.next().value;
const obj2 = gen.next().value;
weakMap.set(obj1, 'stored value1');
weakMap.set(obj2, 'stored value2');
const result = [...gen].map(obj => weakMap.get(obj));
console.log(result);
SmartCode
CHALLENGE const weakMap = new WeakMap(); const gen = (function* () { yield { key: 'value1' }; yield { key: 'value2' }; })(); const obj1 = gen.next().value; const obj2 = gen.next().value; weakMap.set(obj1, 'stored value1'); weakMap.set(obj2, 'stored…
Output :
Anonymous Quiz
50%
[]
8%
[null,null]
33%
[Undefiend,Undefined0]
8%
[stored value1, stored value2]
CHALLENGE
const weakMap = new WeakMap();
const array = [{}, {}];
array.forEach(obj => weakMap.set(obj, obj));
const result = array.map(obj => weakMap.get(obj) === obj);
console.log(result);
CHALLENGE
const weakMap = new WeakMap();
const obj = {};
(function() {
const internalObj = {};
weakMap.set(internalObj, 'hidden');
obj.ref = internalObj;
})();
delete obj.ref;
const result = weakMap.has(obj.ref);
console.log(result);
CHALLENGE
const weakMap = new WeakMap();
const array = [1, 2, 3];
const obj = {};
weakMap.set(obj, array);
const result = weakMap.get(obj).reduce((acc, val) => acc + val);
console.log(result);
CHALLENGE
const weakMap = new WeakMap();
const objs = [{}, {}, {}];
objs.forEach((obj, index) => weakMap.set(obj, index + 1));
const result = objs.filter(obj => weakMap.has(obj)).map(obj => weakMap.get(obj) * 2);
console.log(result);