diff --git a/1064.fixed-point.js b/1064.fixed-point.js new file mode 100644 index 00000000..93f18bdb --- /dev/null +++ b/1064.fixed-point.js @@ -0,0 +1,17 @@ +/* URL of this problem + * https://leetcode.com/problems/fixed-point/description/ + * + * @param {number[]} arr + * @return {number} + */ + +var fixedPoint = function(arr) { + for (let i = 0; i < arr.length; i++) { + if (i === arr[i]) { + return i; + } + } + return -1; +}; + +module.exports = fixedPoint; \ No newline at end of file diff --git a/fixedPoint.test.js b/fixedPoint.test.js new file mode 100644 index 00000000..5486f6ca --- /dev/null +++ b/fixedPoint.test.js @@ -0,0 +1,21 @@ +const fixedPoint = require("./1064.fixed-point"); + +test("Return the smallest index if it exists in the input arr", () => { + expect(fixedPoint([-10,-5,0,3,7])).toBe(3); +}); + +test("Return -1 if an index that satisfies 'arr[i] == i' does not exist in the input arr", () => { + expect(fixedPoint([-10,-5,0,3,7])).toBe(3); +}); + +test("Return -1 if the input arr is empty", () => { + expect(fixedPoint([])).toBe(-1); +}); + +test("Return -1 if the input arr is made of Infinity", () => { + expect(fixedPoint([Infinity, Infinity, Infinity])).toBe(-1); +}); + +test("Return -1 if the input arr is made of -Infinity", () => { + expect(fixedPoint([-Infinity, -Infinity, -Infinity])).toBe(-1); +}); \ No newline at end of file