Tell us what’s happening:
Describe your issue in detail here.
Giving expected output but I am not able to pass the test. Can anyone please guide me what I am doing wrong here. I know there is other way to solve this problem but its my own algorithm and trying to learn by practice.
Your code so far
function diffArray(arr1, arr2) {
const newArr = [];
let arr = arr1.concat(arr2);
let output = arr
.reduce(function(acc,curr){
if(acc[curr]){
acc[curr] = ++acc[curr];
}else{
acc[curr] =1;
}
return acc;
},{});
//console.log(output);
for (const keys in output){
if(output[keys]==1){
newArr.push(String(keys));
}
}
return newArr;
}
console.log(diffArray([1, "calf", 3, "piglet"], [1, "calf", 3, 4]));
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36
Challenge: Intermediate Algorithm Scripting - Diff Two Arrays
The output value of numbers are in string, that might be the reasons that is not allowing to pass the test.
However, I have removed the ‘String’ from this line of code, ‘newArr.push(String(keys));’ but its giving the same result, I even try to convert to Integer but the string items giving NaN and there is no way to test if its number or string because all the keys are in string . So I think its not right approch to solve the problem.
It works for these tests, but I’d consider if you can think of an alternative approach or a modification. This won’t work if the data is a boolean, for example.