Skip to content

Commit 6c17abe

Browse files
authored
Create faster.js
1 parent 0fb9b52 commit 6c17abe

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

faster.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
*
3+
* More complex yet faster way to log "Hello World".
4+
*
5+
**/
6+
let target = "hello world";
7+
let index = 0;
8+
let pointer = 0;
9+
let ans = "";
10+
let chars = "abcdefghijklmnopqrstuvwxyz".split('');
11+
let System = { out: { println: (...args) => console.log(...args) } };
12+
13+
function log(char) {
14+
return new Promise(resolve => {
15+
setImmediate(() => {
16+
let cur = target.split('')[pointer];
17+
18+
if (cur === " ") {
19+
pointer++;
20+
ans += " ";
21+
}
22+
23+
if (char === cur) {
24+
ans += char || " ";
25+
pointer++;
26+
}
27+
28+
let toLog = ans + char;
29+
30+
System.out.println(toLog.endsWith("dd") ? toLog.slice(0, -1) : toLog);
31+
32+
if (ans === target) {
33+
System.out.println("Successfully logged 'hello world!'");
34+
process.exit();
35+
}
36+
37+
resolve();
38+
});
39+
});
40+
}
41+
42+
async function main() {
43+
while (pointer < target.length) {
44+
let i = chars[index];
45+
await log(i);
46+
index = (index + 1) % chars.length;
47+
}
48+
}
49+
50+
main().catch(err => console.error(err));

0 commit comments

Comments
 (0)