Help with where art thou

I’m sorry, but our learning styles are too different for me to be able to help. I don’t know how to teach someone how to code by coaching them through deconstructing the solutions of others.

I spent 6 hours on this problem. I did everything I could not to look at the solution, but at some point theres a diminishing returns on the effort I’m putting in. At one point people will wake up and not look forward to coding, they’ll procrastinate, many even quit. Keep in mind I even spent a month going back to do the past sections once I reached this point a month ago. Thank you for understanding that I know myself enough to have to make these tough decisions. Maybe you can handle more hours of struggle than me, but everyone has a breaking point

Deconstructing the code of others is an important skill but is a different skill than coding. It’s cool that you want to learn how to deconstruct the code of others instead of learning how to write code. That’s just not something that I know how to teach to someone who isn’t already comfortable with how to write code.

I think that looking at solution is good as long as you understand it.

https://www.w3schools.com/jsref/jsref_filter.asp

It may be not clear, filter gets every element (arr[0], arr[1]...).
It checks single element (arr[0] > 15).
If true element is added to the new array.

Hi @am93 !

Welcome back!

I feel like most people will decide to quit because programming is hard.

It is totally fine if it takes a while for you to get comfortable with algorithms.
Most people struggle with them when they are first learning.

If you want more practice with fundamentals, I think building some small javascript projects will help.

I took some of the basic algorithms and created small little projects around them.

1 Like

@JeremyLT ok fair enough you’ve been helpful regardless.

@MyTrueName yeah exactly I thought I’d still try to learn from this experience and ask those two questions. Why isn’t source[keys[i]] also logging 3 again?

@jwilkins.oboe Hi! Yeah I imagine it’s when they struggle with a problem for too long and feel hopeless. That is a worse outcome for me than looking at a solution to understand the code. Though I know that’s definitely still not an ideal outcome.

True I can see how projects could help. It’s been a few months since I even did the html/css stuff and I’m afraid I may have forgotten those too. But ideally I’d finish the JS section I’m on then do projects. I would also be curious as to what is the best website for that type of practice at my skill level. I heard of this website but not sure if it’s the most fit https://www.frontendmentor.io/ What do you think?

The projects look cool but it looks like you have to pay to get all of the features.

You might try JavaScript 30 which is free

1 Like

I tried to keep as much as I could from the intended logic from your first post, but major changes were necessary to make it work. Note that the outer loop is for...of (because you need the whole object of the collection array), and the inner loop is for...in (because you only need the property names of the source object):

function whatIsInAName(collection, source) {
  var arr = [];

  // create an outer loop for the collection objects
  for (var obj of collection){

    // assume that the current object fulfills all requirements to be pushed into the result array
    let isValid = true;

    // now check if that's actually true by looping over the source object
    for (var property in source) {
        
        // if the object doesn't have the current source property,
        // or if it has such a property but values aren't equal - set isValid to false
        if (!obj.hasOwnProperty(property) || (obj.hasOwnProperty(property) && obj[property] !== source[property])){
          isValid = false;
        }
    }

    // once the loop is done, check if isValid is still true
    if (isValid){
      arr.push(obj)
    }
  }

  return arr;
}
1 Like

Try to work this from inside out.

[i] is a number, let’s say 5
keys[5] ← this notation assumes that keys is:

  • an array (in which case it looks for position 5 of an array)
    OR
  • an object (in which case it looks for key ‘5’ in an object)

If it returns anything:
source[thingThatWasReturned].
And we are checking if source is an array or object, same as with keys.

Was that helpful?

I don’t understand. What I’m asking is when I console.log(source[keys[i]]); I get 1 and 9999 not 3. I see you’re explaining its either an object or a array, but I’m just wondering why it’s not returning 3

  console.log(source)
  console.log(keys)
  console.log(keys[1])
  console.log(source[keys[1]])

Put it after your keys declaration.

I have done that. I’m console logging in the loop

So you have values of source, keys, and keys[1] written out.

What is the value of keys[1]?

{ a: 1, b: 9999, c: 3 }
[ ‘a’, ‘b’, ‘c’ ]
b
9999

so b

Then source['b'] should be?

9999 so I’m just wondering why keys[2] and 3 are not being logged when I console log in the loop

Because of the return.

The loops stops when the condition !object.hasOwnProperty(keys[i]) || object[keys[i]] !== source[keys[i]] is true.

function whatIsInAName(collection, source) {
  let keys = Object.keys(source);
  //console.log(keys);
  return collection.filter(function (object) {
    for (let i = 0; i < keys.length; i++) {
      console.log(source[keys[i]]); // 1, 9999
      const condition =
        !object.hasOwnProperty(keys[i]) || object[keys[i]] !== source[keys[i]];
      console.log(condition); // false, true
      if (condition) {
        console.log(condition); // true
        return false;
      }
    }
    return true;
  });
}
console.log(whatIsInAName([{ a: 1, b: 2, c: 3 }], { a: 1, b: 9999, c: 3 }));
1 Like

^^ exactly.
Then I am leaving you in the hands of a pro. : )

1 Like

@lasjorg @MyTrueName thank you!

2 Likes

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