Running BufferUnderflow
The Simulation Code#
BufferUnderflow is ready to go! Let's write a simulation in our app.ts
:
1
import Question from './entities/Question';
2
import Answer from './entities/Answer';
3
import Image from './entities/Image';
4
import User from './entities/User';
5
6
const user1 = new User('Wonda', User.Permission.Normal);
7
const user2 = new User('Carlos', User.Permission.Normal);
8
const admin = new User('Danny', User.Permission.Admin);
9
10
const question = new Question(
11
user1,
12
'How can you convert any value to a boolean in JS?',
13
[new Image('Example', Image.Format.PNG, 'url.com/fake.png')],
14
);
15
16
const answers = [
17
new Answer(
18
user2,
19
question,
20
'Use two logical NOT operators: !!value',
21
),
22
new Answer(
23
admin,
24
question,
25
'Use the Boolean constructor: Boolean(value)',
26
),
27
];
28
29
question.setAnswer(answers[0].getId(), user1); // OK
30
31
try {
32
// Error: User must be an admin or the author of question to answer
33
question.setAnswer(answers[1].getId(), user2);
34
} catch (e) {}
35
36
try {
37
// Error: Answer not found. Double check the answer ID
38
question.setAnswer('randomId', admin);
39
} catch (e) {}
40
41
question.setAnswer(answers[1].getId(), admin); // OK
42
43
const openingLine = '~~~~~~~~~~ BufferUnderflow ~~~~~~~~~~';
44
const questionSummary = question.getSummary();
45
const answerSummaries = answers.map(a => a.getSummary());
46
47
const ui = [openingLine, questionSummary, answerSummaries]
48
.join(`\n\n${'='.repeat(50)}\n\n`);
49
50
console.log(ui);
The Output#
Running our app.ts
file will produce the following output in the terminal:
1
~~~~~~~~~~ BufferUnderflow ~~~~~~~~~~
2
3
==================================================
4
5
Question: How can you convert any value to a bo
6
Author:
7
- Wonda (2463581) [0 qs, 0 ans]
8
Attachments: 1
9
Answers: 2
10
Has an answer been chosen? Yes (4017239)
This page is a preview of Beginners Guide to TypeScript