Skip to content

Commit 70eb9b0

Browse files
committed
make low level program code
1 parent 4c15872 commit 70eb9b0

File tree

1 file changed

+121
-35
lines changed

1 file changed

+121
-35
lines changed

src/main.js

Lines changed: 121 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,159 @@
11
import fs from 'fs'
22
import path from 'path'
33
import { fileURLToPath } from 'url'
4+
import { exit } from 'process'
45
import chalk from 'chalk'
56
import readlineSync from 'readline-sync'
67

78

89

910

11+
// Simplify std out from `console.log` into `print`
12+
const print = console.log
13+
14+
1015
/**
1116
* TODO:
1217
*
13-
* - Just complete the program flow based on some console.log below [0/7]
18+
* - Just complete the program flow based on some print below [0/7]
1419
*/
1520

1621

1722
const dataDir = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'data')
1823

24+
const showGuide = () => {
25+
print('This is a guide for this program!');
26+
for (let i = 1; i <= 5; i++) {
27+
print('Data: ', i)
28+
}
1929

20-
let confirmation = 0
21-
do {
22-
console.log(`
23-
==========================
24-
WELCOME!
25-
==========================
26-
SIMPLE LINEAR REGRESSION DEMO
27-
`)
28-
29-
// 1
30-
console.log('Show menu')
3130

32-
console.log(`
33-
==========================
34-
1. Guide
35-
2. Run demo
36-
==========================
37-
`)
31+
if (!readlineSync.keyInYNStrict(chalk.green('Back?'))) {
32+
exit(0)
33+
}
34+
}
3835

36+
const runDemo = () => {
3937
let files
4038
try {
41-
console.log('Scanning data input file ...')
39+
print('Scanning data input file ...')
4240
files = fs.readdirSync(dataDir)
4341

44-
console.log('Choose file to do simple demo linear regression: ')
42+
print('Choose file to do simple demo linear regression: ')
4543
files.forEach(file => {
46-
console.log(chalk.blue('- ', file))
44+
print(chalk.blue('- ', file))
4745
})
4846
} catch (error) {
49-
console.error(chalk.red('Error reading directory: ', error.message))
47+
print(chalk.red('Error reading directory: ', error.message))
48+
return;
5049
}
5150

51+
if (readlineSync.keyInYNStrict(chalk.green('Preview data?'))) {
52+
previewData('data passed')
53+
}
5254

53-
// 2
54-
console.log('Preview data')
55+
}
56+
57+
const previewData = (data) => {
58+
print('Preview data: ', data)
59+
60+
if (readlineSync.keyInYNStrict(chalk.green('Continue next process?'))) {
61+
dataIdentification()
62+
}
63+
}
64+
65+
66+
67+
68+
const dataIdentification = () => {
69+
print('Data identification!')
70+
const feature = readlineSync.question('Choose feature: ')
71+
const label = readlineSync.question('Choose label: ')
72+
73+
if (readlineSync.keyInYNStrict(chalk.green('Continue process data?'))) {
74+
processing()
75+
}
76+
}
77+
78+
79+
80+
81+
const processing = () => {
82+
print('Model processing!')
83+
for (let i = 1; i <= 20; i++) {
84+
print('iteration: ', i)
85+
}
86+
87+
if (readlineSync.keyInYNStrict(chalk.green('Show visualization: '))) {
88+
visualization()
89+
}
90+
}
91+
92+
5593

56-
// 3
57-
console.log('Choose feature')
5894

59-
// 4
60-
console.log('Choose target')
6195

62-
// 5
63-
console.log('Processing...')
96+
const visualization = () => {
97+
print('Visualization here!')
98+
print('Graph, chart, or whatever...')
6499

65-
// 6
66-
console.log('Show the linear equation result')
67100

68101
// 7
69-
console.log('Show the prove of linear equation is actually match with user data')
102+
print('Show the prove of linear equation is actually match with user data')
103+
}
104+
105+
106+
107+
108+
109+
110+
111+
/////////////////////////////////////////////////////////////////////
112+
//// 6MMMMb\ MMMMMMMMMM dM. `MMMMMMMb. MMMMMMMMMM /////
113+
//// 6M' ` / MM \ ,MMb MM `Mb / MM \ /////
114+
//// MM MM d'YM. MM MM MM /////
115+
//// YM. MM ,P `Mb MM MM MM /////
116+
//// YMMMMb MM d' YM. MM .M9 MM /////
117+
//// `Mb MM ,P `Mb MMMMMMM9' MM /////
118+
//// MM MM d' YM. MM \M\ MM /////
119+
//// MM MM ,MMMMMMMMb MM \M\ MM /////
120+
//// L ,M9 MM d' YM. MM \M\ MM /////
121+
//// MYMMMM9 _MM_ _dM_ _dMM__MM_ \M\_ _MM_ /////
122+
/////////////////////////////////////////////////////////////////////
70123

71-
confirmation = readlineSync.questionInt('Continue? ')
72-
} while (confirmation == 0);
124+
do {
125+
console.clear()
126+
print(`
127+
==========================
128+
WELCOME!
129+
==========================
130+
SIMPLE LINEAR REGRESSION DEMO
131+
==========================
132+
-----------MENU-----------
133+
==========================
134+
1. Guide
135+
2. Run demo
136+
3. Exit
137+
==========================
138+
`)
139+
140+
const selectedMenu = readlineSync.questionInt(chalk.green('Choose menu: '))
141+
console.clear()
142+
143+
switch (selectedMenu) {
144+
case 1:
145+
showGuide()
146+
break;
147+
case 2:
148+
runDemo()
149+
break;
150+
case 3:
151+
print('Bye!')
152+
exit(0);
153+
break;
154+
default:
155+
print('Invalid input! Menu can not be found!')
156+
break;
157+
}
73158

159+
} while (readlineSync.keyInYNStrict(chalk.green('\nContinue program? ')))

0 commit comments

Comments
 (0)