Challenge Diff Two Arrays has an issue.
User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36
.
Please describe how to reproduce this issue, and include links to screenshots if possible.
My code:
function diff(arr1, arr2) {
var newArr = [];
newArr = arr1.concat(arr2);
console.log("arr total is: ",newArr);
//count occurence of item in array
//return number of occurrence > 0
var count = function(item, array){
var count = 0;
var i=0;
for (i ; i<array.length; i++){
if (array[i] == item){
count++;
}
}
console.log("This item: "+item+" appears: "+ count+" times!");
return count;
};
arr = [];
var y=0;
var restOfArr = newArr.slice(0);
for (y; y<newArr.length; y++){
if (restOfArr.length == 0){
console.log("EMPTY. RES IS: ", arr);
return arr;
}
//console.log("index is: ", y);
var item = restOfArr[0];
console.log("item is: ", item);
restOfArr = restOfArr.slice(1);
var counter = count(item, restOfArr);
if (counter == 0){
arr.push(item);
restOfArr = restOfArr.filter(function(el){
return el !== item;
// console.log("after delete item restOfArr is: ", restOfArr);
});
}
if (counter > 0){
//ce ne sono 2 non addo
restOfArr = restOfArr.filter(function(el){
return el !== item;
});
}
console.log("rest of arr: ", restOfArr);
// console.log("FOR: arr is: "+arr+" newArr is: "+newArr);
}
return arr;
}
diff([1, 2, 3, 5], [1, 2, 3, 4, 5]);
I am also having an issue with this - my code is passing the challenges but they are not becoming green.
Code below:
`function diff(arr1, arr2) {
var newArr = [];
// Same, same; but different.
newArr = arr1.concat(arr2);
newArr.sort();
var ans = [];
var i = 1;
for (i=1; i
ans.push(newArr[i]);
}
}
return ans.sort();
}
diff(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]);`
@ninjasun thanks for the issue but I don't think it is necessary to add a test with duplicate elements in one array. This challenge IMO is on testing you to implement symmetric differences, which is actually a method on (mathematical) sets, which don't have duplicates. @SaintPeter thoughts?
@vaspv your code isn't working because we have been updating function names recently. Change your function diff
to diffArray
and then you should start seeing some checkmarks.
Thanks Eric, working now.
I tried to pass different values in the array and it is working, but I am not able to clear this challenge.
If I am doing something wrong, any help would be appreciated.
I know there may be better ways of solving this particular challenge but right now I'm interested in what am I missing? or is it some kind of a bug?
function diff(arr1, arr2) {
var newArr = [];
// Same, same; but different.
for (var i = 0;i<arr1.length;i++) {
if(arr2.indexOf(arr1[i]) === -1) {
newArr.push(arr1[i]);
}
}
for(var j=0;j<arr2.length;j++) {
if(arr1.indexOf(arr2[j]) === -1) {
newArr.push(arr2[j]);
}
}
return newArr;
}
diff(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]);
Hi as per Eric above - Change your function diff to diffArray and then you should start seeing some checkmarks.
Ok it worked, although it din't make any sense.
@erictleung For this earlier bonfire I don't know that we need to have corner cases like duplicate elements in one array.
@erictleung I think duplicates would be good, it would allow a user to discover that these are to be treated like sets. Also, it doesn't really make sense to think of them as sets, since arrays impose ordering, and I thought the ordering was significant, which is where the difficulty came in. If a user thinks ordering matters, it seems like it will follow a levenshtein style of solution, which makes it unnecessarily complicated. Another option would be to describe it in more accessible language like "Return the elements in only the first array followed by the elements in only the second"
I'd advocate these tests:
// the element in array 2 fulfills every element in array 1
diffArray([1, 1], [1]); // => []
// order doesn't matter, like a set
diffArray([1, 2], [2, 1]); // => []
// comparison does not get confused by types (eg I put them in as object keys to cache lookup, but that casts them to strings, so my solution was wrong)
diffArray([1], ['1']); // => [1, '1']
Most helpful comment
@ninjasun thanks for the issue but I don't think it is necessary to add a test with duplicate elements in one array. This challenge IMO is on testing you to implement symmetric differences, which is actually a method on (mathematical) sets, which don't have duplicates. @SaintPeter thoughts?
@vaspv your code isn't working because we have been updating function names recently. Change your function
diff
todiffArray
and then you should start seeing some checkmarks.