Solution feedback - Diff Two Arrays

Tell us what’s happening:
Here I am again asking for feedback hahaha, or maybe just validation? Anyway I was wondering how my solution looked. After I solved it and checked the solutions I felt I probably should have done things similar to the first solution but I ended up using includes because it was in the relevant docs thing on the hints page so I looked at it and used it. I did changeMy !arr1.includes(element), originally it was arr1.includes(element) == false after looking at the solution that included it because it looks better I believe. Anyway feedback is appreciated as always and it feels like I might be asking for feedback after each problem at this rate.

Your code so far

function diffArray(arr1, arr2) {
// merges both arrays into one array
const mergeList  = [].concat(arr1, arr2);
// Compares the values to the original arrays and filter any differences into the newArr
  const newArr = mergeList.filter((element) => !arr1.includes(element) || !arr2.includes(element));
  //returns the new array with the difference
  return newArr;
}

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.0

Challenge: Intermediate Algorithm Scripting - Diff Two Arrays

Link to the challenge:

The main thing I’d say is get rid of all the comments. Two of them are really unnecessary (they are just stating the obvious). I suppose a case could be made for keeping the middle comment but it can be simplified quite a bit and you don’t need to tell us that you are putting the results in newArr as we can see that from the code.

I don’t mean to sound too harsh here. Your code is fine. But excessive comments can make it harder to read.

Also, you could do away with both the mergeList and newArr variables and write this as one return statement. I know the default code includes the newArr variable, so it seems like you have to use it. But if you want to be really concise, you don’t need any temp variables since all the array methods you are using return a new array.

Thanks for the feedback and I understand the comments aren’t exactly necessary, they are mainly for me since I download the solution and might want to go back to review. I also didn’t think about just using the one return statement either haha, I guess it still feels weird to just have it all in one return statement instead of just having everything in one var and returning that.

Using intermediate variables can sometimes help readability but then the variable names have to be well thought-out and contextual. I know you didn’t add the newArr variable but it is a good example of a variable name that is neither.

newDataType lacks context and calling it new does not mean the old wasn’t mutated. Filtering produces a subset of the original data, the context is what that subset is. The fact that it is “new” is explicit in the non-mutating method used.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.