-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-magic-8-ball.js
More file actions
130 lines (108 loc) · 2.85 KB
/
3-magic-8-ball.js
File metadata and controls
130 lines (108 loc) · 2.85 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
Let's peer into the future using a Magic 8 Ball!
https://en.wikipedia.org/wiki/Magic_8-Ball
There are a few steps to being able view the future though:
* Ask a question
* Shake the ball
* Get an answer
* Decide if it's positive or negative
The question can be anything, but the answers are fixed,
and have different levels of positivity or negativity.
Below are the possible answers:
## Very positive
It is certain.
It is decidedly so.
Without a doubt.
Yes - definitely.
You may rely on it.
## Positive
As I see it, yes.
Most likely.
Outlook good.
Yes.
Signs point to yes.
## Negative
Reply hazy, try again.
Ask again later.
Better not tell you now.
Cannot predict now.
Concentrate and ask again.
## Very negative
Don't count on it.
My reply is no.
My sources say no.
Outlook not so good.
Very doubtful.
*/
// This should log "The ball has shaken!"
// and return the answer.
function shakeBall() {
//Write your code in here
}
/*
This function should say whether the answer it is given is
- very positive
- positive
- negative
- very negative
This function should expect to be called with any value which was returned by the shakeBall function.
*/
function checkAnswer(answer) {
//Write your code in here
}
/*
==================================
======= TESTS - DO NOT MODIFY =====
There are some Tests in this file that will help you work out if your code is working.
To run these tests type `node 3-magic-8-ball.js` into your terminal
==================================
*/
const log = console.log;
let logged;
console.log = function () {
log(...arguments);
logged = arguments[0];
};
function test(test_name, expr) {
let status;
if (expr) {
status = "PASSED";
} else {
status = "FAILED";
}
logged = undefined;
console.log(`${test_name}: ${status}`);
}
const validAnswers = [];
function testAll() {
const answer = shakeBall();
test(
`shakeBall logs "The ball has shaken!"`,
logged === "The ball has shaken!"
);
test(`shakeBall returns an string answer`, typeof answer === "string");
test(
`checkAnswer("It is decidedly so.") returns "very positive`,
checkAnswer("It is decidedly so.") === "very positive"
);
test(
`checkAnswer("My reply is no.") returns "very negative`,
checkAnswer("My reply is no.") === "very negative"
);
test(
`checkAnswer returns the level of positivity"`,
["very positive", "positive", "negative", "very negative"].includes(
checkAnswer(answer)
)
);
const answers = new Set();
for (let i = 0; i < 10; ++i) {
answers.add(shakeBall());
}
test(`shakeBall returns different answers`, answers.size > 1);
test(
`checkAnswer returns different answers`,
new Set(Array.from(answers.values()).map(checkAnswer)).size > 1
);
}
testAll();