Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions 2229.check-if-an-array-is-consecutive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* URL of this problem
* https://leetcode.com/problems/check-if-an-array-is-consecutive/description/
*
* @param {number[]} nums
* @return {boolean}
*/

var isConsecutive = function(nums) {
const SortedNums = [...nums].sort((a, b) => a - b);
const RangeStart = Math.min(...nums);
const RangeEnd = RangeStart + nums.length - 1;
const RangeNums = [];

for (let i = RangeStart; i <= RangeEnd; i++) {
RangeNums.push(i);
}

return SortedNums.join("") === RangeNums.join("");
};

module.exports = isConsecutive;
13 changes: 13 additions & 0 deletions isConsecutive.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const isConsecutive = require("./2229.check-if-an-array-is-consecutive");

test("Return true if the input nums contains all the integers in the range", () => {
expect(isConsecutive([1,3,4,2])).toBeTruthy();
});

test("Return false if the input nums misses any integer in the range", () => {
expect(isConsecutive([1,3])).toBeFalsy();
});

test("Return false if the input nums contains Infinity", () => {
expect(isConsecutive([1,3,4,2,Infinity])).toBeFalsy();
});