Skip to content

Commit 252e1a0

Browse files
AntonovIgorkam4atka
authored andcommitted
Добавит исходники примеров
1 parent 1f51ef5 commit 252e1a0

File tree

314 files changed

+31861
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

314 files changed

+31861
-2
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
console.log(`Hello, world from Node ${process.version}`);

01-basic/04-hello-world/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Глава 1.4 Привет, Мир
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
const utils = require(`./utils`);
4+
5+
const alphabet = [`А`, `B`, `C`, `D`, `E`, `F`, `G`];
6+
7+
// Тасование массива
8+
console.log(`Тасование массива:`);
9+
console.log(utils.shuffle(alphabet));
10+
11+
// Получение случайного числа
12+
console.log(`Получение случайного число от 1 до 10`);
13+
console.log(utils.getRandomInt(1, 10));
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
/**
4+
* Перетасовка массива по алгоритму
5+
* Фишера—Йетса.
6+
*
7+
* Функция возвращает новый массив
8+
*
9+
* @param {Array} array
10+
* @return {Array}
11+
*/
12+
const shuffle = (array) => {
13+
const resultArray = array.slice();
14+
for (let i = resultArray.length - 1; i > 0; i--) {
15+
const randomNumber = Math.floor(Math.random() * (i + 1));
16+
[resultArray[randomNumber], resultArray[i]] = [resultArray[i], resultArray[randomNumber]];
17+
}
18+
19+
return resultArray;
20+
};
21+
22+
/**
23+
* Возвращает случайное число в диапазоне
24+
* `min` и `max`.
25+
*
26+
* @param {Number} min
27+
* @param {Number} max
28+
* @return {Number}
29+
*/
30+
const getRandomInt = (min, max) => {
31+
min = Math.ceil(min);
32+
max = Math.floor(max);
33+
return Math.floor(Math.random() * (max - min + 1)) + min;
34+
};
35+
36+
module.exports = {
37+
getRandomInt,
38+
shuffle,
39+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
const simpleModule = require(`./simple-module`);
4+
5+
console.log(simpleModule);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
// Экспортируем myProperty со значением 1
4+
module.exports.myProperty = 1;
5+
6+
// Экспортируем фунцию foo
7+
module.exports.foo = () => console.log(`Hello from function`);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict';
2+
3+
exports.foo = () => console.log(`This is first function`);
4+
exports.anotherFoo = () => console.log(`This if second function`);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict';
2+
3+
const simpleModule = require(`./exports`);
4+
console.log(simpleModule);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
const simpleModule = require(`./simple-module`);
4+
5+
console.log(simpleModule);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
const foo = (text) => console.log(text);
4+
const anotherFoo = () => console.log(`This is another func`);
5+
6+
module.exports = {
7+
foo,
8+
};
9+
10+
exports.anotherFoo = anotherFoo;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
console.log(`Hello from test module!`);
4+
5+
module.exports = {
6+
test: () => console.log(`Hello, world!`),
7+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const fs = require(`fs`);
4+
5+
const customRequire = (modulePath) => {
6+
const resolvePath = require.resolve(modulePath);
7+
8+
if (customRequire.cache[resolvePath]) {
9+
return customRequire.cache[resolvePath].exports;
10+
}
11+
12+
const moduleCode = fs.readFileSync(resolvePath, `utf-8`);
13+
14+
const module = {id: resolvePath, exports: {}};
15+
customRequire.cache[resolvePath] = module;
16+
17+
const wrapperFunction = Function(`require, exports, module`, moduleCode);
18+
wrapperFunction(customRequire, module.exports, module);
19+
20+
21+
return customRequire.cache[resolvePath].exports;
22+
};
23+
24+
customRequire.cache = {};
25+
26+
27+
const exampleModule = customRequire(`./05-example.js`);
28+
exampleModule.test();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
require(`./module-a`);
4+
require(`./module-b`);
5+
require(`./module-c`);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
console.log(`Hello from Module A`);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
require(`./module-a`);
4+
5+
console.log(`Hello from Module B`);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
require(`./module-a`);
4+
5+
console.log(`Hello from Module C`);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { sum, hello } from './07-simple-module.mjs';
2+
3+
console.log(sum(1, 2));
4+
hello(`User`);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const sum = (a, b) => a + b;
2+
export const hello = (name) => console.log(name);

01-basic/07-modules/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Глава 1.7 Модули
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
const [nodePath, appPath] = process.argv;
4+
console.log(`Путь к Node: ${nodePath}`);
5+
console.log(`Путь к текущему сценарию: ${appPath}`);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict';
2+
3+
const readline = require(`readline`);
4+
const chalk = require(`chalk`);
5+
6+
const welcomeText = `
7+
Добро пожаловать в игру
8+
«Угадай число»
9+
10+
|\\---/|
11+
| o_o |
12+
\\_^_/
13+
14+
Привет, я Кекс. Мне нравится загадывать числа.
15+
Всё честно: вы назовёте максимальное число, а я
16+
загадаю случайное число в диапазоне от нуля до предложенного вами числа.
17+
18+
Попробуйте его угадать. Количество попыток неограничено.`;
19+
20+
const readLineInterface = readline.createInterface({
21+
input: process.stdin,
22+
output: process.stdout,
23+
});
24+
25+
const getRandomNumber = (min, max) => {
26+
min = Math.ceil(min);
27+
max = Math.floor(max);
28+
return Math.floor(Math.random() * (max - min)) + min;
29+
};
30+
31+
const showWinMessage = (secretNumber) => {
32+
console.log(chalk.magenta(`
33+
|\\---/|
34+
| o_o | Ура! Вы угадали число.
35+
\\_^_/ Я действительно загадал ${secretNumber}.
36+
`));
37+
38+
readLineInterface.close();
39+
};
40+
41+
const checkAnswer = (secretNumber) => {
42+
readLineInterface.question(chalk.blueBright(`Ваш ответ: `), (inputNumber) => {
43+
const userAnswer = Number.parseInt(inputNumber, 10);
44+
45+
if (secretNumber === userAnswer) {
46+
return showWinMessage(secretNumber);
47+
}
48+
49+
console.log(chalk.redBright(`Промазал. Попробуй ещё.`));
50+
return checkAnswer(secretNumber);
51+
});
52+
};
53+
54+
const startGame = () => {
55+
console.log(chalk.green(welcomeText));
56+
57+
readLineInterface.question(chalk.bgYellow.red(`Максимальное число: `), (maxNumber) => {
58+
const myNumber = getRandomNumber(0, Number.parseInt(maxNumber, 10));
59+
checkAnswer(myNumber);
60+
});
61+
};
62+
63+
startGame();

01-basic/08-cli/07-guess-the-number-with-chalk/package-lock.json

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "guess-the-number-with-chalk",
3+
"version": "1.0.0",
4+
"description": "A simple console game",
5+
"main": "07-guess-the-number-with-chalk.js",
6+
"scripts": {
7+
"start": "node ./07-guess-the-number-with-chalk.js",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"author": "HTML Academy",
11+
"license": "MIT",
12+
"dependencies": {
13+
"chalk": "^3.0.0"
14+
}
15+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
3+
const readline = require(`readline`);
4+
5+
const welcomeText = `
6+
Добро пожаловать в игру
7+
«Угадай число»
8+
9+
|\\---/|
10+
| o_o |
11+
\\_^_/
12+
13+
Привет, я Кекс. Мне нравится загадывать числа.
14+
Всё честно: вы назовёте максимальное число, а я
15+
загадаю случайное число в диапазоне от нуля до предложенного вами числа.
16+
17+
Попробуйте его угадать. Количество попыток неограничено.`;
18+
19+
const readLineInterface = readline.createInterface({
20+
input: process.stdin,
21+
output: process.stdout,
22+
});
23+
24+
const getRandomNumber = (min, max) => {
25+
min = Math.ceil(min);
26+
max = Math.floor(max);
27+
return Math.floor(Math.random() * (max - min)) + min;
28+
};
29+
30+
const showWinMessage = (secretNumber) => {
31+
console.log(`
32+
|\\---/|
33+
| o_o | Ура! Вы угадали число.
34+
\\_^_/ Я действительно загадал ${secretNumber}.
35+
`);
36+
37+
readLineInterface.close();
38+
};
39+
40+
const checkAnswer = (secretNumber) => {
41+
readLineInterface.question(`Ваш ответ: `, (inputNumber) => {
42+
const userAnswer = Number.parseInt(inputNumber, 10);
43+
44+
if (secretNumber === userAnswer) {
45+
return showWinMessage(secretNumber);
46+
}
47+
48+
console.log(`Промазал. Попробуй ещё.`);
49+
return checkAnswer(secretNumber);
50+
});
51+
};
52+
53+
const startGame = () => {
54+
console.log(welcomeText);
55+
56+
readLineInterface.question(`Максимальное число: `, (maxNumber) => {
57+
const myNumber = getRandomNumber(0, Number.parseInt(maxNumber, 10));
58+
checkAnswer(myNumber);
59+
});
60+
};
61+
62+
startGame();

01-basic/08-cli/07-hello-user.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
const [nodePath, appPath, commandName, userName] = process.argv;
4+
console.log(`------------------`);
5+
console.log(`Hello, ${userName}`);
6+
console.log(`Путь к Node: ${nodePath}`);
7+
console.log(`Путь к текущему сценарию: ${appPath}`);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
const TIMEOUT = 0;
4+
5+
console.log(`Этот текст будет выведен...`);
6+
setTimeout(
7+
() => console.log(`А этот никогда`),
8+
TIMEOUT
9+
);
10+
11+
process.exit();

0 commit comments

Comments
 (0)