It passes the challenge so it is correct. I think your algorithm starts off just fine by concatenating the arrays but I think it gets a little messy in the for loop. It obviously works, but I think it can be a little simpler and cleaner. Think about what needs to be done here. You have a concatenated array that includes all of the elements from arr1 and arr2. You want to return an new array that only includes items that are not found in both of the original arrays. In other words:
item is not in arr1 OR item is not in arr2
Does this look like some boolean logic to you? And I bet you can find an easier method for checking if an item is in an array.
No, it’s not the same thing. You already know that an item in the concatenated array will always be in one of the two original arrays. So by your logic above the test will always be true because the item will either be in arr1 or in arr2.
What you want is to test if the item is not in one of the arrays. Then you know it’s in one but not the other and you should return it.
Tricky though - you’re using the length of the concatenated array as the exit condition, but then you’re changing the length. Might work here, but this is a troublesome practice to get into.
In functional programming, you’ll rarely see the use of imperative style. It happens, sure, but it can be avoided.
My point was, you’re indexing your array by an i variable, and you’re looping until that index equals or exceeds the length of the array. With this, in many cases, your i will point to an index just beyond the array eventually, or cause some unintentional strangeness.
Rather than an or logic, i wonder about “not…and” logic: “only include this item if it is not in both array 1 and array 2.”
If it is in one or the other, then it is not in both, returning true.
But at this moment the function is not know since it 's built after the line two. Why it’s not a problem here. I believed that functions must be build before being called, but maybe I’m wrong
Not clear at all, I don’t understand, I think I’m trouble by the association of :
[... // this is the "rest"
and
diff(arr1, arr2), // this is the function diff()
So “rest” can be used with a function, but I didn’t see that in the lessons. I looked on mdn site and I didn’t see this kind of using.
I will open another topic maybe tomorrow but you can let me a last comment about my last message if you want and I will see it tomorrow.
Sorry, laziness on my part. That was meant to indicate “the function body here as in the example” without my typing it out.
My point was, when using the function keyword to definea function, javascript places that function in a var (rather than a let or const) type variable. And var type variables actually are declared when the function is initially called, rather then in the line where they first appear.
In this case the ... is actually the spread syntax not the rest parameter.
Spread syntax turns an iterable (such as an array in this case) into a list that can be used to populate an array. Since the diff function returns an array the spread syntax can be used to incorporate the returned array into another array. So:
[...diff(arr1, arr2), ...diff(arr2, arr1)];
is merely “spreading” the arrays returned by the two calls to diff into one array. It is no different than if you did:
[...[0,1,2], ...[3,4,5]]
Which gives you the array [ 0, 1, 2, 3, 4, 5 ]. The solution just happens to be calling the diff function to get the arrays it wants to spread.
Thank you for this comment. I don’t know why I didn’t understand yesterday. I wasn’t well aware of the concatenation. Now it’s clear.
For you last message, I understand all. Just what is trouble again is the fact that “diif” is used in this line:
So before than the function is declared.
First of all, could you confirm me that normally a function must be declared before being used ?
And after explain to me what is special in this case.
This isn’t really true in JavaScript with functions declared with the function keyword in this fashion. So long is the function is declared somewhere in scope, you can call the function (either before or after the definition is written).
If we had instead declared the function with this syntax