Alright, I’ll try to explain my thought-process as best as I can.
1. Understand the problem
I start by reading the problem multiple times and understand that what the challenge wants me to do is filter out the items in the array that did not match in arr1
and arr2
. So for example, in arr1
has ['a','b','c']
and arr2
has ['a','b','c','d']
. There is no 'd'
item in arr1
, so this item has to go to new array. This much I understood.
2. Draw the roadmap
Usually, I look at the hints to provide me some clues on how to design the path to the solution. The tests did not restrict what methods to use to solve so I’m free to use the one I’m most comfortable with - for loop. Based on the hints, it will go something like this:
/**
// Create a variable containing an empty array
// Create the loop to check if item in array1 is not equal to array2
// When the item is not equal to both arrays, push that item into a new empty array
// In the same loop, check if item in array2 is not equal to array1.
// Return the filtered array
*/
With this initially thought out plan, I’m going to split the problem and write pseudocodes.
3. Split the problem into small tasks and solve each task
At first, I was thinking of using filter()
and worked out the examples from docs and blog post in Relp. Since I cannot think of a solution using the filter()
, I decided to go with the for
loop route.
This path may be longer than if I were to use high order function, but I can see the logical steps which I find it more important than writing less codes:
/**
// Create a variable containing an empty array
variable no match = empty array
// Create the loop to check if item in array1 is not equal to array2
for loop variable j 0, if j is less than 0, increment j
// When the item is not equal to both arrays, push that item into a new empty array
if array1 item is not equal to array2, push item to variable no match
// In the same loop, check if item in arr2 is not equal to arr1.
if array2 item is not equal to array1, push item to variable no match
// Return the filtered array
return variable no match
*/
This should be fairly straightforward, at least to me. After logging the output so that I can see what the results would look like. I’m ready to write real codes.
4. Replace pseudocode with real codes
(Now this is the point where the problems starts to surface. It’s one thing to solve the logical problem through a pseudocode, it’s another thing when it’s time to implement it using real codes. You’ll see it shortly.)
For this challenge, how do I actually go on and check the items if it is not equal to array? What method do I have to use to compare the items against the arrays?
After googling, I figure out that I can do it using the ==
operator. I remember that I can use !
to see if the value is not equal to another value. Therefore in order to check it, I’d have to write if (arr2[j] !== arr1)
. I did the same thing in the second if statement by swapping arr2
with arr1
.
Unfortunately, this returned not the way I expected. When I tweaked (arr2[j] !== arr1)
into (arr2[j] !== 0)
, (arr2[j] !== -1)
and so on, I get various results but not the one I am looking for.
When this happens, I often go back to read errors, change the codes, test it again, then check the docs, googling the errors, and so on.
At this point, I stopped to think logically and before I know it, I’m already powering through the problem with trial and error and this is how I end up the mess.