1+ // Don't change this file. The tests check your refactored calculateMode still behaves the same.
2+
13const calculateMode = require ( "./mode.js" ) ;
24
35// Acceptance criteria for calculateMode function
@@ -6,10 +8,15 @@ const calculateMode = require("./mode.js");
68// When calculateMode is called on the array
79// Then it should return the number that appears most frequently in the array
810
9- // Example:
10- // Given [2,4,1,2,3,2,1]
11- // When calculateMode is called on [2,4,1,2,3,2,1]
12- // Then it should return 2 */
11+ // E.g. calculateMode([2, 4, 1, 2, 3, 2, 1]) returns 2
12+
13+ // Given an empty array
14+ // When calculateMode is called on the array
15+ // Then it should throw Error("calculateMode requires a non-empty array")
16+
17+ // Given something that isn't an array of numbers, e.g. "banana" or [1, "2", 3]
18+ // When calculateMode is called on it
19+ // Then it should throw Error("calculateMode requires an array of numbers")
1320
1421describe ( "calculateMode()" , ( ) => {
1522 test ( "returns the most frequent number in an array" , ( ) => {
@@ -24,9 +31,27 @@ describe("calculateMode()", () => {
2431 expect ( calculateMode ( nums ) ) . toEqual ( 2 ) ;
2532 } ) ;
2633
27- test ( "ignores non-number values" , ( ) => {
28- const nums = [ 1 , 3 , "2" , 2 , 3 , null ] ;
34+ test ( "throws when the array contains a non-number" , ( ) => {
35+ expect ( ( ) => calculateMode ( [ 1 , 3 , "2" , 2 , 3 , null ] ) ) . toThrow (
36+ new Error ( "calculateMode requires an array of numbers" )
37+ ) ;
38+ } ) ;
39+
40+ test ( "throws when given something that isn't an array" , ( ) => {
41+ expect ( ( ) => calculateMode ( "banana" ) ) . toThrow (
42+ new Error ( "calculateMode requires an array of numbers" )
43+ ) ;
44+ } ) ;
45+
46+ test ( "throws when given no argument" , ( ) => {
47+ expect ( ( ) => calculateMode ( ) ) . toThrow (
48+ new Error ( "calculateMode requires an array of numbers" )
49+ ) ;
50+ } ) ;
2951
30- expect ( calculateMode ( nums ) ) . toEqual ( 3 ) ;
52+ test ( "throws when given an empty array" , ( ) => {
53+ expect ( ( ) => calculateMode ( [ ] ) ) . toThrow (
54+ new Error ( "calculateMode requires a non-empty array" )
55+ ) ;
3156 } ) ;
3257} ) ;
0 commit comments