# Array isSubset toy challenge — Javascript

So I’m taking a Javascript class and I want to document the logic behind the solutions for several challenge problems to help me remember.

So the challenge problem is:

Create a function that takes two arrays and determines whether the second array is a subset of the first array. Arrays will only contain primitive values.

I am not at the point yet where I could figure this out on my own, so I turned to StackOverflow to see if I could identify a way to figure out solving it on my own. I searched and searched and search and finally found this post: http://stackoverflow.com/questions/8628059/check-if-every-element-in-one-array-is-in-a-second-array. All the way down at the bottom, a post by qw3n that got zero votes, ended up giving me the answer in the simplest form possible. Thank you qw3n, you are my hero!

So I took that code and decided to deconstruct it so I can actually understand how it works. This is my explanation to myself.

First off, here is the solution code:

`function isSubset(array, sub) {`
`   var x = sub.length, i=0, c;`
`    if (x > array.length){`
`         return false;              }`
`   else {`
`     for(i; i<x; i++) {`
`      c=array.indexOf(sub[i]);        if (c > -1) {          array.splice(c,1);       }`
`     else {`
`          return false;`
`       }       return true; `
`   }}`

So first since this post is for me and I’m a noob, let’s break down the elements of the solution.

1. array and sub are parameters of the isSubset function. They are essentially placeholder variable names.
2. var x = sub.length, i = 0, c; is the same as writing var x = sub.length, var i = 0, and var c.
3. var x = sub.length gives us the number values in an array. We use this because we don’t know how many values will be in an array.
4. var i = 0 is setting the index variable to start at index 0. This is for our loop. We don’t need to define this at this point, it can also be set when writing the loop. How it is done is personal preference.
5. var c is basically a placeholder variable and we will define it later which is why it doesn’t have an assignment at the beginning.

Now that we have some variables set we start out with an if statement.

`if (x > array.length) {     return false;  }`

Let’s break this statement down.

What this statement is saying is if (conditions are true) then run what is in the code block { }. In this case, we would return false.

x > array.length means that if x (which is the variable we set to sub.length) is greater than the length of the array parameter then return false. The logic behind this is is the subset array (parameter named sub) is longer than the array parameter, then there is no way it is a subset of the parameter named array. Therefore we can return false immediately rather than iterating through the entire array.

So now we need need to tell the function what to do if the sub parameter is not longer than the array parameter. For this we use an else statement.

`else {`
`for(i; i<x; i++) {`
`c=array.indexOf(sub[i]);        if (c > -1) {          array.splice(c,1);       }`
`else {`
`return false;`
`}       return true;`
`}`

Let’s now breakdown the else statement.

1. for (i; i<x; i++) — This is the standard syntax for iterating through a loop. Best to just memorize it.
2. c = array.indexOf(sub[i]) — indexOf is a Javascript method. It searches the array it is attached to for the specified item and returns its value.

What array.indexOf(sub[i]) is doing is searching the parameter array for the value at whatever index of sub the loop is on and returns its position. If no item is found, it returns a -1. Returning -1 is a function of the indexOf method. You do not need to code it to do this, it does it automatically.

Then we ask if c > -1. If c is greater than -1, then that means we essentially have a match for that iteration and can keep going through the loop.

array.splice is used to remove that positive match from the array for the next loop iteration. (c, 1) tells the splice method to start at c, and remove 1. It will go through the loop and if c is always greater than -1 then we will return true.

If at any point we get a -1 meaning that c was not greater than -1, that means we have a value in the subset array (sub) that does not match a value in the array parameter, so we tell it to return false.

If you read this and find a flaw in my logic, please correct me!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.