-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-magic-8-ball.js
More file actions
118 lines (96 loc) · 2.98 KB
/
3-magic-8-ball.js
File metadata and controls
118 lines (96 loc) · 2.98 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
/**
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 the tests for just this one file, type `npm test -- --testPathPattern 3-magic-8-ball` into your terminal
(Reminder: You must have run `npm install` one time before this will work!)
==================================
*/
test("whole magic 8 ball sequence", () => {
const consoleLogSpy = jest.spyOn(global.console, "log");
const answer = shakeBall();
expect(typeof answer).toEqual("string");
expect(consoleLogSpy).toHaveBeenCalledTimes(1);
expect(consoleLogSpy).toHaveBeenLastCalledWith("The ball has shaken!");
expect(checkAnswer(answer)).toBeOneOf([
"very positive",
"positive",
"negative",
"very negative",
]);
});
test("magic 8 ball returns different values each time", () => {
const seenAnswers = new Set();
for (let i = 0; i < 10; ++i) {
seenAnswers.add(shakeBall());
}
if (seenAnswers.size < 2) {
throw Error(
"Expected to get different random answers each time shakeBall was called, but always got the same one"
);
}
let seenPositivities = new Set(Array.from(seenAnswers.values()).map(checkAnswer));
if (seenPositivities.size < 2) {
throw Error(
"Expected to random answers with different positivities each time shakeBall was called, but always got the same one"
);
}
});
test("checkAnswer works for `It is decidedly so.`", () => {
expect(checkAnswer("It is decidedly so.")).toEqual("very positive");
});
test("checkAnswer works for `My reply is no.`", () => {
expect(checkAnswer("My reply is no.")).toEqual("very negative");
});