SmartCode
1.06K subscribers
145 photos
2 videos
20 files
130 links
Download Telegram
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);
Channel photo removed
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);
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);
CHALLENGE


const obj1 = {
name: "Alice",
age: 25
};

const obj2 = {
age: 30,
city: "Wonderland"
};

with (obj1) {
with (obj2) {
name = "Bob";
console.log(name, age);
}
}