-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion.ts
74 lines (64 loc) · 2.65 KB
/
question.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* @module Question
*/
import { type Answer } from "./answer.js";
/**
* This class is used to create objects to represent a quiz question. It consists of a `string` prompt (the question
* being asked) and an array of {@link Answer}s.
*
* @see {@link Answer}
*/
export class Question
{
readonly #promptText: string;
readonly #answers: readonly Answer[];
readonly #string: string;
/**
* Constructs a new {@link Question} object instance with it's {@link Question.promptText} and
* {@link Question.answers} properties set to the passed `string` and `readonly Answer[]` arguments respectively.
*
* @param prompt The `string` literal value set to the created object's {@link Question.promptText} property.
*
* @param answers The array of {@link Answer} objects to set to the constructed object's instance
* {@link Question.answers} property.
*
* @throws {@link TypeError} if either or both passed arguments are `undefined` or `null`.
*
* @throws {@link Error} If passed {@link Answer}s array is empty and doesn't contain a correct answer.
*/
public constructor(prompt: NonNullable<string>, answers: NonNullable<readonly Answer[]>)
{
if (prompt === undefined || prompt === null)
{
throw new TypeError(`${new.target.name}: ${prompt} prompt string.`);
}
if (answers === undefined || answers === null)
{
throw new TypeError(`${new.target.name}: ${answers} answers array.`);
}
if (answers.length === 0)
{
throw new Error(`${new.target.name}: answers array is empty.`);
}
if (answers.every(answer => answer.isCorrect === false))
{
throw new Error(`${new.target.name}: no correct answers provided.`);
}
this.#promptText = prompt;
this.#answers = Object.isFrozen(answers) ? answers : Object.freeze(Array.from(answers));
this.#string = `${new.target.name} {prompt: "${this.#promptText}", answers: [${this.#answers.map(answer => `{answer: "${answer.text}", isCorrect: ${answer.isCorrect}}`).join(", ")}]}`;
}
/**
* This {@link Question} object's prompt text `string` property.
*/
public get promptText(): string { return this.#promptText; }
/**
* Returns the array of {@link Answer}s of this {@link Question} object.
*/
public get answers(): readonly Answer[] { return this.#answers; }
/**
* Returns a `string` representation of this {@link Answer} object.
* @returns a `string` representation of this {@link Answer} object.
*/
public toString(): string { return this.#string; }
}