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