Currently, I am practicing JavaScript concepts by working on various LeetCode problems that I don't normally encounter in my daily work routine.
While starting with the easy section, I encountered an issue with merging arrays. I realized that I rarely use the splice method because I tend to iterate over elements and return a new array instead of modifying the original one. Additionally, I usually work with smaller datasets that do not require direct modification.
When testing with Jasmine, I encountered the following error:
Check for Sorted Merge
✗ Array values are merged and sorted - Expected $.length = 6 to equal 3. Expected $[2] = 2 to equal 3. Unexpected $[3] = 3 in array. Unexpected $[4] = 5 in array. Unexpected $[5] = 6 in array.
Below is the code snippet I was working on:
//////////////////
// INSTRUCTIONS //
//////////////////
// Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
// The number of elements initialized in nums1 and nums2 are m and n respectively.
// You may assume that nums1 has a size equal to m + n such that it has enough space to hold additional elements from nums2.
const nums1 = [1, 2, 3];
const m = 3;
const nums2 = [2, 5, 6];
const n = 3;
const mergeArray = (nums1, nums2) => {
for (let index = 0; index < nums1.length - 1; index++) {
if (nums2[index] >= nums1[index] && nums2[index] < nums1[index+1] ) {
nums1.splice(index, 0, nums2[index]);
}
}
return nums1;
};
module.exports = function () {
describe("Check for Sorted Merge", () => {
it("Array values are merged and sorted", () => {
expect(nums1.concat(nums2).sort()).toEqual(mergeArray(nums1, nums2));
});
});
};