From 1a0a68852c2d030e267786cb4a1bbdff046d0556 Mon Sep 17 00:00:00 2001 From: Hidemichi Shimura Date: Tue, 27 Dec 2022 20:10:03 -0800 Subject: [PATCH 1/2] solve 1064. Fixed Point --- 1064.fixed-point.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 1064.fixed-point.js 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 From ed9b11f81a6b218f7f8cb590f7ae3bb57fdf977d Mon Sep 17 00:00:00 2001 From: Hidemichi Shimura Date: Tue, 27 Dec 2022 20:10:36 -0800 Subject: [PATCH 2/2] test function fixedPoint --- fixedPoint.test.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 fixedPoint.test.js 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