CHALLENGE
const scores = [85, 92, 78, 90];
const student = {
name: 'Jordan',
grade: 'A',
...{ courses: ['Math', 'Science'] },
scores,
average: function() { return this.scores.reduce((a, b) => a + b) / this.scores.length }
};
const { name, ...details } = student;
const [first, ...rest] = scores;
console.log(details.scores[0], rest[0]);
β€5π₯2
π₯3π2π€2β€1
CHALLENGE
function* createCounter() {
let count = 0;
while (true) {
const reset = yield ++count;
if (reset) {
count = 0;
}
}
}
const counter = createCounter();
console.log(counter.next().value);
console.log(counter.next().value);
console.log(counter.next(true).value);
console.log(counter.next().value);
β€9π2π€©1
π5
CHALLENGE
function createCounter() {
let count = 0;
const counter = {
increment() {
count++;
return count;
},
getCount() {
return count;
}
};
return counter;
}
let c1 = createCounter();
c1.increment();
c1.increment();
let c2 = c1;
c1 = null;
console.log(c2.getCount());
β€3π1π₯1
What is the output?
Anonymous Quiz
21%
null
27%
undefined
21%
ReferenceError: count is not defined
31%
2
π€6π5β€1π₯1
In June we featured php-node, a new way to βbridge the gapβ between PHP and Node.js by being able to embed PHP into Node apps. Now theyβve gone a step further by using php-node and the Watt app server to enable the running of Laravel apps too. A curious meeting of ecosystems!
Stephen Belanger (Platformatic)
Please open Telegram to view this post
VIEW IN TELEGRAM
β€4π2π₯1π€1
CHALLENGE
const weakSet = new WeakSet();
const obj1 = { name: 'First' };
const obj2 = { name: 'Second' };
const obj3 = obj1;
weakSet.add(obj1);
weakSet.add(obj2);
let result = '';
result += weakSet.has(obj1) + ', ';
result += weakSet.has(obj3) + ', ';
obj2.name = 'Modified';
result += weakSet.has(obj2) + ', ';
weakSet.delete(obj1);
result += weakSet.has(obj3);
console.log(result);
β€7π₯1
What is the output?
Anonymous Quiz
17%
true, false, true, true
29%
true, true, true, undefined
41%
true, true, true, false
13%
true, false, true, false
π₯6π3β€2
npq performs several extra steps compared to npm. It consults Snykβs database of vulnerabilities, looks at the packageβs age, download count, and docs, and tries to paint a better picture of what youβre really installing.
Liran Tal
Please open Telegram to view this post
VIEW IN TELEGRAM
π₯5π4β€2
CHALLENGE
class LightMachine {
constructor() {
this.states = {
green: { next: 'yellow' },
yellow: { next: 'red' },
red: { next: 'green' }
};
this.currentState = 'green';
}
transition() {
this.currentState = this.states[this.currentState].next;
return this.currentState;
}
}
const lightMachine = new LightMachine();
let result = '';
for (let i = 0; i < 5; i++) {
result += lightMachine.transition() + ' ';
}
console.log(result.trim());
β€4π1
What is the output?
Anonymous Quiz
46%
yellow red green yellow red
22%
yellow red green red yellow
14%
red green yellow red green
18%
green yellow red green yellow
β€8π1
Please open Telegram to view this post
VIEW IN TELEGRAM
π€£61π₯4β€2π2
CHALLENGE
async function processValues() {
try {
console.log('Start');
const a = await Promise.resolve('First');
console.log(a);
const b = await Promise.reject('Error');
console.log(b);
return 'Done';
} catch (err) {
console.log(err);
return 'Recovered';
} finally {
console.log('Finally');
}
}
processValues().then(result => console.log(result));
β€1
What is the output?
Anonymous Quiz
30%
Start First Error Finally Recovered
24%
Start First Finally Recovered
39%
Start First Error Recovered Finally
7%
Start First Finally Error Recovered
π4β€1π₯1
CHALLENGE
function greet(name) {
return `Hello, ${name}!`;
}
function highlight(strings, ...values) {
return strings.reduce((result, str, i) => {
return result + str + (values[i] ? `<em>${values[i]}</em>` : '');
}, '');
}
const user = 'Sarah';
const status = 'online';
console.log(highlight`User ${user} is currently ${status}.`);
π₯2β€1
What is the output?
Anonymous Quiz
20%
User <em>Sarah</em> is currently online.
43%
User <em>Sarah</em> is currently <em>online</em>.
15%
`User ${user} is currently ${status}.`
22%
User Sarah is currently online.
β€2π2π₯2
A meaty article (which took a year to put together) covering the myriad of JavaScript runtimes and engines both past and present, from obvious picks like Node.js to cloud platforms and lesser known βhonorable mentionsβ. This is a great summary to round out your JS ecosystem knowledge.
Whatever, Jamie
Please open Telegram to view this post
VIEW IN TELEGRAM
π6β€5
CHALLENGE
class ChatServer {
constructor() {
this.clients = new Set();
this.messageLog = [];
}
connect(client) {
this.clients.add(client);
return () => this.clients.delete(client);
}
broadcast(message, sender) {
this.messageLog.push(message);
this.clients.forEach(client => {
if (client !== sender) {
client.receive(message);
}
});
}
}
const server = new ChatServer();
const david = { name: 'David', receive: msg => console.log(`David got: ${msg}`) };
const sarah = { name: 'Sarah', receive: msg => console.log(`Sarah got: ${msg}`) };
const emma = { name: 'Emma', receive: msg => console.log(`Emma got: ${msg}`) };
const disconnectDavid = server.connect(david);
server.connect(sarah);
server.connect(emma);
server.broadcast('Hello everyone!', david);
disconnectDavid();
server.broadcast('Is David still here?', sarah);
console.log(server.clients.size);
β€2π1
β€3π2π₯2