Intermediate Algorithm Scripting - IF statement

I dont understand the differences between these 2 codes and why the 1st code produced the true answer and didnt meet the same problems as the 2nd code.
Please help me:

1st one:

function whatIsInAName(collection, source) {
  const art = [];
  for (let i = 0; i < collection.length; i++) {
    let check = true;
    for (const prop in source) {
      if (collection[i][prop] !== source[prop]) {
        check = false;
      }
    }
    if (check) art.push(collection[i]);
  }
  return art;
}

console.log(whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1}, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 }));

2nd code:

function whatIsInAName(collection, source) {
  const art = [];
  for (let i = 0; i < collection.length; i++) {
    for (const prop in source) {
      if (collection[i][prop] !== source[prop]) {
      } else {
        art.push(collection[i]);
      }
    }
  }
  return art;
}

console.log(whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1}, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 }));

Challenge: Intermediate Algorithm Scripting - Wherefore art thou

Link to the challenge:

1 Like

The second argument can be an object with more than one property/value. If that’s the case then all of the property/values in the second argument must match before the item can be included in the returned array. Does that help you see why the second function won’t work?

1 Like

I knew it but I think the 1st code works the same way, which makes me dont understand the differences between these 2 codes

if i want to compare two objects to be identical:
Object_1 = { a : 1 , b : 2 , c : 3 }
Object_2 = { a : 1 , b : 2 , c : 3 }

Both properties a, b ,c should be SAME.

  • Object_1.a == Object_2.a
  • Object_1.b == Object_2.b
  • Object_1.c == Object_2.c

if any of them not same, then Not identical. Or simple word, AND operation.

You should have some kind of True/False variable to remember your last comparison. Else, it looks an OR operation.

2 Likes

I think i understand now. The point is the position of push(collection[i]);
The 2nd code push(collection[i]) everytimes there’s a property that makes collection[i][prop] == source[prop] so it’s obviously wrong.
Meanwhile the 1st code looks through the whole collection[i] and only return check = true when every property in 2nd argument matches. Only when meeting that condition, the code push(collection[i]) later.

I find it out when trying to understand what you said. Thank you so much.

1 Like

Okay i’ve got confused now. I made a little change in the order of true/false as well as the condition, and it’s not work

function whatIsInAName(collection, source) {
const art = ;
for (let i = 0; i < collection.length; i++) {
let check = false;
for (const prop in source) {
if (collection[i][prop] == source[prop]) {
check = true;
}
}
if (check) art.push(collection[i]);
}
return art;
}

now the console display
[ { apple: 1, bat: 2 },
{ apple: 1 },
{ apple: 1, cookie: 2, bat: 2 } ]

:frowning:

I made a little change in the order of true/false as well as the condition, and it’s not work

let check = false;
for (const prop in source) {
if (collection[i][prop] == source[prop]) {
check = true;
}

now the console display

[ { apple: 1, bat: 2 },
{ apple: 1 },
{ apple: 1, cookie: 2, bat: 2 } ]

Can you explain this for me?
:frowning:

You are almost there.

Just add some logic to mark your variable to False and exit the For-loop , once it encounters un-matched property.

I still didnt figure it out. Can you help me please :frowning:

Try add this to bottom of your code, as debugging text:

let result = whatIsInAName([{ “apple”: 1, “bat”: 2 }, { “apple”: 1 }, { “apple”: 1, “bat”: 2, “cookie”: 2 }], { “apple”: 1, “cookie”: 2 })

console.log(result)

Then add this two lines to your if-else logic

check = false;
break;

Oh I didnt know about break;
Thank you very much!

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