Coding task · JavaScript and React course · Module 13: Testing React
Practice: Testable Functions
A pure function always returns the same result for the same arguments and changes nothing outside itself, so a unit test checks it with one expect: expect(calculateFuelConsumption(100, 50)).toBe(3). An error can be tested too: expect(() => calculateFuelConsumption(100, 0)).toThrow(). The test also checks the boundary values of the range, because that is where bugs hide most often.
Task
Complete two pure navigation functions that the tests check (you see the results in the test panel, and the preview shows a mini PASS/FAIL report). ___BLANK1___: calculateFuelConsumption throws an error when the speed is not greater than zero (0 or a negative number). ___BLANK2___: the function returns the fuel consumption as in the lesson: distance divided by speed, times 1.5 (calculateFuelConsumption(100, 50) gives 3). ___BLANK3___: isValidCoordinate returns true only for a number from -1000 to 1000 inclusive, and false for everything else: numbers out of range, NaN, Infinity, strings such as '42', null and undefined.