Some() method not working as expected

Tell us what’s happening:
The console.log(found) outputs 3 times true. It should be 2 times —for number 1 and 2— because the number 3 from args[0] doesn’t exist in args[1].

  **Your code so far**

function sym(...args) {

let flattened = args.reduce((accumulator, currentValue) => accumulator.concat(currentValue), []);
let countInstances = flattened.reduce(function (allNumbers, number) {
  if (number in allNumbers) {
  else {
    allNumbers[number] = 1;
  return allNumbers
}, {})
let newArr = []

for (const number in countInstances) {
  if (`${countInstances[number]}` == 1) {
    newArr.push(parseInt(number, 10))
  } else if (`${countInstances[number]}` >= 2) {
    const found = args[0].some(number => args[1].includes(number))


sym([1, 2, 3, 3], [5, 2, 1, 4]);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36

Challenge: Find the Symmetric Difference

Link to the challenge:

Hi @Porphyrogennitos

I have no idea what you are trying to do here.

  1. Look at number which is a property of countInstances and number which is an argument to the callback to .some Array method. Is that intentional?
  2. Why are you comparing a string and a number?

I haven’t run your code but by reading it I suspect this is what is happening

  1. countInstances will look like {1: 2, 2: 2, 3: 2, 4: 1, 5: 1} after running .reduce.
  2. You will be looping through countInstances in the loop. The else if block is then executed 3 times when number is 1, 2, and 3. Each time there is a number in args[0] which is found in args[1].

number variable in const found = args[0].some(number => args[1].includes(number)) is not the same as this one here for (const number in countInstances) {.

use this and see if the values you see let you understand what’s going on

const found = args[0].some(number => {
     console.log({'args[1]': args[1], number, 'args[1].includes(number)': args[1].includes(number)})
     return args[1].includes(number)

I’m checking if a number from args[0] exists in args[1].

  1. Yes. Is this wrong?
  2. Because I couldn’t find where to implement the parseInt() function. :sweat_smile:

Yeah, that’s exactly what it looks like.

  1. Yeah. Although number 3 appears 2 times, these 2 times are in args[0] so it should pass the symmetric difference function. This is what I’m trying to fix.
  2. I know that but number 3 is not found in args[1] and I don’t understand why the some() method returns true when checking number 3. :man_shrugging:

Wait, what? Why? Is it because the number in const found is not an integer?

Oh! So I have to put the const found inside an iteration!

it’s inside a loop, but the things you use to check, args[0] and args[1], do not depend on the loop, so you are just doing the same thing over and over - number will always be each element inside args[0], and so the result of args[1].includes(number) will always be the same

Any hint on how to fix this?

I don’t really understand what algorithm you are following here
What logic steps are you using to get the desired results?

In the above example, args[0] has 2 times the number 3 but args[1] doesn’t contain it. So number 3 should be returned from the symmetric difference function. I wrote the some() function because I want to check if a number that exists more than one time in args[0] also exists in args[1]. If it does, it won’t be returned.

but some will return true if any number in args[0] is included in args[1]

you need to make so that your output will include any number included in the first array but not in the second and included in the second but not in the first, and it can’t have duplicates

I think you are making it a bit too complex than necessary

Let’s see on a step at a time
How do you get an array with the elements included in the first array but not in the second?

We could use the filter() method to check if the elements of the first array are not present in the second array?

Try and see if it works, this is a practice Algorithm!

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