Wherefore-art-thouu

Wherefore-art-thouu
0

#1

I am not sure why this code is doing what it is doing. When attempting to submit it for the challenge it doesn’t pass the second test. I manually loaded the test array into my editor and ran it in my browser to see what the problem was, but what I am seeing doesn’t really make sense to me. the first and second collection array objects are both {“apple”:1}, and while the test passes the first one it fails the second one. Any help understanding what I am doing wrong in my setup would be appreciated. Thank you.

function whatIsInAName(collection, source) {
    // Setting up the variables
    var arr = [];
    var sTerm = JSON.stringify(source);    
    sTerm = sTerm.replace(/[{}]/g, "");      
    var regexp = new RegExp(sTerm, 'gi');
    
    //The for loop that will determine
    // if any of the collection elements
    //pass the test and if so will add then 
    //to the arr[]
    for(var x in collection){
        console.log(collection[x]);
        let str = JSON.stringify(collection[x]);
        console.log(str);
        result = regexp.test(str);
        console.log(result); 
        if(result){
          arr.push(collection[x]);
        } 
        
      
      }
    
    // Only change code above this line
    return arr;
  }
  
  console.log(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/63.0.3239.132 Safari/537.36.

Link to the challenge:
https://beta.freecodecamp.org/en/challenges/intermediate-algorithm-scripting/wherefore-art-thou


#2

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.