-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
37 lines (28 loc) · 827 Bytes
/
index.ts
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
29
30
31
32
33
34
35
36
37
import { readFile } from 'node:fs/promises'
import { resolve } from 'node:path'
async function miniCompiler() {
let text = ''
try {
const filePath = resolve('./message_02.txt')
text = await readFile(filePath, { encoding: 'utf8' })
} catch (error) {
console.log('This is error -> ', error)
}
const listSymbols = text.split('')
const operations: Record<string, (count: number) => number> = {
'&': (count: number) => count,
'#': (count: number) => count + 1,
'@': (count: number) => count - 1,
'*': (count: number) => count * count,
}
let count = 0
let result = ''
listSymbols.forEach(symbol => {
count = operations[symbol](count)
result += symbol === '&' ? count : ''
})
console.log(result)
}
;(async () => {
await miniCompiler() // result -> 024899455
})()