3h Tonight was about one third kata, one third podcasts, and one third coding on my projects. I did a kata called Array.diff that is sorting through one array and removing elements if they are found in a second array. That seems simple enough. I wrote it, but it ended up being rather long for the functionality.
// https://www.codewars.com/kata/523f5d21c841566fde000009/train/javascript
function array_diff(a, b) {
let matchValues = []
for (let i = 0;i<b.length; i++){
for (let j = 0; j<a.length; j++){
if (a[j]===b[i]){
matchValues.push(j)
}
}
}
matchValues.sort(function(a, b){return a-b})
let deduped_set = new Set(matchValues)
matchValues= [...deduped_set]
return strip_array(a,matchValues)
}
function strip_array(array, list) {
for (let i = list.length; i>0; i--){
let match=list.pop()
array.splice(match,1)
}
return array
}
Then I saw that the top answer was 3 lines long! oof. Here it is.
function array_diff(a, b) {
return a.filter(function(x) { return b.indexOf(x) == -1; });
}
So what that little nugget is doing is filtering a with a function that goes through each element in a and checks if it is in b. if it is not then it is added to the output. I’ve never used a filter method like this before, but I’m definitely going to keep an eye out for this in the future. Thankfully the method name is appropriate so I can try to remember that what I am doing is filtering, so I should try a filter method on it.
As excited as I was about the built-in atom git integration last night I have discovered today that it seems like commits made that way seem to not show up in the graph. I’m not a vain man, but I’m partially pushing code to keep it alive somewhere, and partially as a way to show my commit(get it?) ment to coding. That green graph is important to me, so I guess I’ll keep using the terminal like our great-grandfathers did 😉