This repository was archived by the owner on Oct 26, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy path3-magic-8-ball.js
More file actions
188 lines (163 loc) · 4.28 KB
/
3-magic-8-ball.js
File metadata and controls
188 lines (163 loc) · 4.28 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
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.
## 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() {
logged="The ball has shaken!";
answers=["Very positive","Positive","Negative","Negative"]
//
//answers[Math.floor(Math.random()*answers.length)]
//console.log(Math.floor(Math.random()*4));
answer=Math.floor(Math.random()*answers.length)
return answer.toString();
}
//answers[Math.floor(Math.random()*answers.length)]
// The answer should come from shaking the ball
//let answer;
}
// This function should say whether the answer it is given is
// - very positive
// - positive
// - negative
// - very negative
function checkAnswer() {
// console.log("ans from main"+answer);
let subAns;
//let no=shakeBall();
//console.log("answer again from checkS"+no);
if(answer==0){
// ");
subAns=["It is certain."
,"It is decidedly so."
,"Without a doubt"
,"Yes - definitely."
,"You may rely on it."];
console.log("very positive");
console.log(subAns[Math.floor(Math.random()*subAns.length)]);
return "very positive";
}
if(answer==1){
subAns=["As I see it, yes."
,"Most likely."
,"Outlook good."
,"Yes."
,"Signs point to yes."];
console.log("positive");
console.log(subAns[Math.floor(Math.random()*subAns.length)]);
return "positive";
}
if(answer==2){
subAns=["Reply hazy, try again."
,"Ask again later."
,"Better not tell you now."
,"Cannot predict now."
,"Concentrate and ask again."];
console.log("negative");
console.log(subAns[Math.floor(Math.random()*subAns.length)]);
return "negative";
}
if(answer==3){
subAns=["Don't count on it."
,"My reply is no."
,"My sources say no."
,"Outlook not so good."
,"Very doubtful."];
console.log("very nagative");
console.log(subAns[Math.floor(Math.random()*subAns.length)]);
return "very negative";
}
=======
// This function should expect to be called with any value which was returned by the shakeBall function.
function checkAnswer(answer) {
}
/* ======= 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(`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();