Wherefore art thou suggestions/improvements

Hi all,
Tell us what’s happening:
here is my working solution to the problem. I wanted to know is there a better solution to this test, also recommend what can I improve in my code or any other suggestions
link to my code: https://repl.it/@ridafatima15h1/Wherefore-art-thou
Thank You all in advance

Your code so far


function whatIsInAName(collection, source) {
  //**Declarations and initializations**

  //array to be filled
  var arr = []; 
  // Only change code below this line
  //while loop counters
  let j = 0; //Outer 
  let k = 0; //inner 
  //filling Arrays for  algorithm
  let  sourceKeys = Object.keys(source);
  let sourceValues = Object.values(source);
  let collectionKeys = [];
  let collectionValues = [];
  //arguments lengths
  let collectionLength = collection.length;
  let sourceLength = sourceKeys.length;
  for(let j = 0; j < collectionLength; j++){
    collectionKeys[j] = Object.keys(collection[j]);
    collectionValues[j] = Object.values(collection[j]);
  }
  //variables for algorithm
  let checkKey = 0;
  let checkValue;

  //**algorithm**
  while(j < collectionLength){
    while(k < sourceLength){
      //console.log("j: ", j); //Debugging
      //console.log("k: ", k); //Debugging
      checkKey = collectionKeys[j].indexOf(sourceKeys[k]);
      //console.log("checkKey: ",checkKey); //Debugging
      if(checkKey === -1){
        k = sourceLength;
        continue;
      }
      checkValue =collectionValues[j][checkKey];
      //console.log("checkValue: ", checkValue); //Debugging
      //console.log("sourceValues[k]: ", sourceValues[k]); //Debugging
      if(checkValue != sourceValues[k]){
        checkKey = -1;
        k = sourceLength;
        continue;
      }
      k++;
    }
    if(checkKey != -1){
      arr.push(collection[j]);
      //console.log("updated arr:", arr); //Debugging
    }
    k = 0;
    j++;
  }

  //**Debugging**
  console.log("___________________________________________________");
  console.log("=> Given Inputs:")
  console.log(" source: ", source);
  console.log(" collection: ", collection);
  console.log();
  console.log("sourceLength: ", sourceLength);
  console.log("collectionLength: ", collectionLength);
  console.log("sourceKeys: ", sourceKeys);
  console.log("sourceValues: ", sourceValues);
  console.log("collectionKeys: ", collectionKeys);
  console.log("collectionValues: ", collectionValues);
  console.log();
  console.log("=> Result Output: ")
  console.log(" final updated arr:", arr); 
  console.log("___________________________________________________");
  // Only change code above this line
  return arr;
}

//Tests
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "cookie": 2 });
whatIsInAName([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 });

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou

Very good, very good.

Your solution works, but as you asked, I came up with old-school retro two nested for as following

function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
let  sourceKeys = Object.keys(source);
for(var a=0;a<collection.length;a++){
  var push_it=false;
  var ca=collection[a];
  for(var b=0;b<sourceKeys.length;b++){
    if(ca[sourceKeys[b]]===source[sourceKeys[b]]){
      push_it=true;
    }else{
      push_it=false;
      break;
    }
  }
  if(push_it){
    arr.push(collection[a]);
  }
}
  return arr;
}

Since each entry is not dependent to another index, more faster way is search in parallel which I don’t think JS comes with threading support(don’t know)

For more faster code, sorting both keys in collection entries and source could help.

keep going on great work, happy programming.

2 Likes

Hi thank you for the feedback!
I think your approach is much better. Using bracket notion to access source properties by name made it much simple. My solution cluttered arrays . Hopefully I will improve.

Yes, many stuffs of JS are so fancy for me, I tend to code the low old form.

I’m not sure, but I think accessing elements by index number is faster(should be) than accessing by key. But as I stated, JS devs not take care about performance and these stuffs so much, as it’s not used for heavy duties(and should not).

keep going on great work, happy programming.

1 Like