Tell us what’s happening:
Can anyone tell why my code isn’t passing the tests?
As clunky as it is my code seems to be returning the correct output for the inputs given. I am copying and pasting each input directly and logging the output.
I have since come up with a better solution using reduce but it would still help to know what’s wrong here.
Your code so far
function sym(...args) {
let sorted = []
let longest = {length:0,i:0}
let result = []
for(let i=0;i < args.length;i++){
let temp = args[i]
if(args[i].length > longest.length){
longest.length = args[i].length
longest.i = i
}
let t = args[i]
let o = Object.fromEntries(t.map(key => [key,key]))
let a = Object.keys(o).map(key => o[key])
sorted.push(a.sort(function(a,b){return a - b}))
}
let a = sorted.shift()
let b = sorted.shift()
result = [findDiff(a,b),...sorted]
if(result.length < 2) {
console.log("ARGUMENTS: ",...args)
console.log("RESULTS",result[0])
return result[0]
}else
sym(...result)
}
function findDiff(a,b){
let result = []
while(a.length > 0 && b.length > 0){
if(a[0] === b[0]){
a.shift()
b.shift()
}else if(a[0] < b[0]){
if(result.indexOf(a[0]) < 0)
result.push(a[0])
a.shift()
}else{
if(result.indexOf(a[0]) < 0)
result.push(b[0])
b.shift()
}
}
result = [...result,...a,...b]
return result
}
sym([1, 2, 5], [2, 3, 5], [3, 4, 5])
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Challenge Information:
Algorithms - Find the Symmetric Difference