• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

miguelriosoliveira / rendezvous-with-cassidoo / 5929943473

21 Aug 2023 06:13PM UTC coverage: 99.796% (-0.2%) from 100.0%
5929943473

push

github

miguelriosoliveira
question: guessingGame

371 of 371 branches covered (100.0%)

Branch coverage included in aggregate %.

52 of 52 new or added lines in 1 file covered. (100.0%)

2073 of 2078 relevant lines covered (99.76%)

8.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.38
/src/2023/314-guessingGame/guessingGame.ts
1
/*
1✔
2
Make a "guessing game" where there is a target number, and as the user makes guesses,
1✔
3
the output returns higher or lower until the user is correct.
1✔
4

1✔
5
Example usage:
1✔
6

1✔
7
Guess the number!
1✔
8
> 10
1✔
9
higher
1✔
10
> 20
1✔
11
higher
1✔
12
> 30
1✔
13
lower
1✔
14
> 25
1✔
15
higher
1✔
16
> 27
1✔
17
Correct! You won in 5 guesses!
1✔
18
*/
1✔
19

1✔
20
/**
1✔
21
 * thanks to https://stackoverflow.com/a/1527820/4916416
1✔
22
 *
1✔
23
 * Returns a random integer between min (inclusive) and max (inclusive).
1✔
24
 * The value is no lower than min (or the next integer greater than min
1✔
25
 * if min isn't an integer) and no greater than max (or the next integer
1✔
26
 * lower than max if max isn't an integer).
1✔
27
 * Using Math.round() will give you a non-uniform distribution!
1✔
28
 */
1✔
29
function getRandomInt(min: number, max: number): number {
×
30
        min = Math.ceil(min);
×
31
        max = Math.floor(max);
×
32
        return Math.floor(Math.random() * (max - min + 1)) + min;
×
33
}
×
34

1✔
35
interface Response {
1✔
36
        success: boolean;
1✔
37
        hint: '>' | '<' | '=';
1✔
38
        guessCount: number;
1✔
39
}
1✔
40

1✔
41
export function guessingGame(randomFunction = getRandomInt): (guess: number) => Response {
1✔
42
        const theNumber = randomFunction(0, 100);
4✔
43
        let guessCount = 1;
4✔
44

4✔
45
        return (guess: number) => {
4✔
46
                if (guess === theNumber) {
6✔
47
                        return { success: true, hint: '=', guessCount };
1✔
48
                }
1✔
49
                guessCount++;
5✔
50
                return { success: false, hint: guess > theNumber ? '<' : '>', guessCount };
6✔
51
        };
6✔
52
}
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc