Katy St Sauveur

Posted on

JavaScript Problem Solving Activity

The challenge: To take one of five problems and solve the problem with a teammate. We were given about an hour to work on the given task. My partner and I worked side by side using Google and Repl to attempt to come to a solution. Here is the problem that we choose to try to conquer:

Merging Arrays:

-Write a function that takes in two Arrays of Numbers. This function should merge the two arrays and return a single array of numbers. The returned array should be sorted from least to greatest.

-It’s ok to assume that only arrays with at least one number will be passed in.

Example:

nums1 = [1, 2, 3, 4, 5];
nums2 = [6, 7, 8, 9, 10];
mergeArrays(nums1, nums2);
// => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

nums3 = [1, 3, 5, 7, 9];
nums4 = [10, 8, 6, 4, 2];
mergeArrays(nums3, nums4);
// => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

The arrays passed in may be sorted in different orders. They may also both store a specific number. Remove duplicated in the returned result.

Example:

nums5 = [1, 3, 5, 7, 9, 11, 12];
nums6 = [1, 2, 3, 4, 5, 10, 12];
mergeArrays(nums5, nums6);
// => [1, 2, 3, 4, 5, 7, 9, 10, 11, 12]

In order to solve our problem we started out by rewriting the problem in pseudo code. We wrote our problem out in a manner that those without any or limited experience in development could read it. Here is what that looked like:

1. Write something that takes in two sets of data that happen to be numbers.
2. What you wrote should merge the two sets of data.
3. This should return a single set of data.
4. The returned set of data should be sorted from least to greatest.
5. The sets of data must have at least one number to be included.
6. Each set of data passed in do not need to have an order.
7. The data cannot be repeated

At that point I came up with a maybe question to focus on:
-Should we maybe store a number in the parameter?

Then we got to work! We created a Repl with two arrays.

var numArray1 = [2, 1, 4, 5, 3];
var numArray2 = [7, 6, 10, 8, 9];

From there we needed to merge our arrays. In order to do that we used the .concat method. See below:

numArray1.concat(numArray2);

Which returned:

[ 2, 1, 4, 5, 3, 7, 6, 10, 8, 9 ]

This got us to step number four from above. We needed to then sort the returned data from least to greatest. We approached this problem using the sort() method which will sort the items of an array. The sort method changes the original array. The sort() method will return an incorrect result when sorting numbers. In order to fix this you can provide a compare function. This is about how far I have come in this challenge, but feel with some additional time I will be able to full solve the problem.

Some reflection thoughts:

It was beyond helpful pseudo coding in the beginning. It almost gave me a checklist for how to solve this problem and definitely a point on where to start. We tried to solve the problem a few times just playing around with the Repl without any real concrete idea on how to solve the problem. This was valuable in isolating what would not work to come to the correct answer. At some point we thought that we had the sort method ready to shoot the correct answer out at us, but upon execution we could recognize that more was needed to solve the issue. So off to Google we went.

The next time that you are given a technical problem I found it to be extremely valuable to write out the issue at hand using basic english. Get rid of the tech terms including functions, arrays, etc. For my execution of the problem it brought it one step closer to a logical solution. If you break down the problem in the beginning you have a more approachable way to find the solution.