vuyisile.github.io

View on GitHub
2 November 2017

Sorted Union

by {"name"=>"Vuyisile Weni"}

Sorted Union.

In Free Code Camp, I am currently completeting Intermediate Algorithm Scripting, now focusing on sorted-union, task number 267, which i found very challenging but looking at the examples of how the output should be, i understood that the function has to combine values that are in the arrays provided as arguments in to one array and the should be no duplicates.

Details:

My Logical thinking about this algorithmic problem.

Understanding that algorithm is set of rules to be followed in calculations, by looking at the solutions I attempted to solve the problem but I failed a couple times until I realized that I did not understand the instructions. So I took a step back and re-read the details and I highlighted the key points about this task, therefore I broke them down for simplicity and did one thing at a time.

As I was highlighting the keys of this problem, the first instruction said I must write a function that takes two or more arrays, from this statement I understood that the function’s arguments are unlimited so the plan is to create a variable that creates an array of arguments passed in the funtion when executed. I moved on to the second bit of the instruction which states that the function should return a new array, in this case I knew I had to create another variable of an empty array (var combo = []), where I will store the all the values from the arrays passed as arguments after iteration. After I all that process, my final array still has duplicates and I realized that in the instructions it was stated that there should be no duplicates, I had to compare the values in the final array so that if the array contains values that are the same or repeated, only one of each is left. The last instruction is that the unique numbers should in the original order, so I left the final array unsorted.

Explanation of my approach in code.

Conclusion.

As I said, I found the problem challenging and because I did not read to understand the question in the first place, so from this problem I’ve learnt that I have to a logical memory of the steps to solve any challenge like this one, so rule number one about solvin a problem is to break down the details and attempt the solving the problem bit-by-bit.

tags: