**Intermediate Algorithm Scripting: Wherefore art thou

Tell us what’s happening:
How may I do access to the values without using strings? I could accomplished whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }) should return [{ first: "Tybalt", last: "Capulet" }] but, I do not hot to stop using strings to access.

Your code so far


function whatIsInAName(collection, source) {
 var arr = [];
 // Only change code below this line
let a=Object.values(collection)
let b= Object.keys(source)
        console.log(a[1])
          console.log(b)

    for(let i=0;i<=collection.length;i++){  
      
      if(a==b |a[i]==b){
                            
                            arr.push(collection[2])
            } 
          
           
          
           
}
console.log(arr)
 // Only change code above this line
 return arr;
}

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/81.0.4044.138 Safari/537.36.

Challenge: Wherefore art thou

Link to the challenge:

why do you think you need to stop using strings to access?

1 Like

Because every test has different strings. How can I do in this case?. What can I read?

I am getting: [ { apple: 1 }, { apple: 1, bat: 2 } ]. I do not know what is the reason why I cannot use <= to do length. It displays Undefined

function whatIsInAName(collection, source) {
  var arr = [];
  // Only change code below this line
let a=Object.values(collection)
 let b= Object.values(source)
                    
       for(let i=0;i<collection.length;i++){

               if(a[i][i]==b[i]){
                     
                      arr.push(collection[i])

                  }



                        
                  
       }        
            
 

  // Only change code above this line
 console.log(arr)
  //return arr;
}

whatIsInAName([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 })

Arrays and Objects are 0 indexed, which means the “count” of each item starts at 0.
So if I have the array [a, b, c]… the array.length would be 3, but the positions would be [0, 1, 2]. You cannot used <= to compare length because array[3] doesn’t exist.

1 Like

and what may I do in this case?


I’ve put together a log so you can see what your loop is checking on each iteration.
Think about what a[i][i] looks at.

1 Like

perfect!. Let met keep reading! Thanks!. I am going to read a little more about arrays and loops.

I could complete the first two challenges, but when I tried to use conditions. It couldn`t get the third one, so what may I do to get the challenge done? What can I read?

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

  // Only change code below this line
//console.log(collection)
//console.log(...collection)
//console.log(source)

//console.log(Object.keys(collection))
//console.log(Object.keys(...collection))
//console.log(Object.values(collection))
//console.log(Object.values(collection)[2])
console.log(Object.values(collection)[2][Object.keys(source)])
//console.log(Object.values(collection)[0][Object.keys(source)])
//console.log(Object.values(...collection))
//console.log(Object.keys(source))
 //console.log(Object.values(collection))
 //console.log(Object.values(source))
 console.log(Object.values(source)[0])

  for(let i=0;i<collection.length;i++){
                             
  if(Object.values(source)[0]==Object.values(collection)[i][Object.keys(source)]){
                       arr.push(Object.values(collection)[i])
                             }
                    }        
  
  // Only change code above this line
       console.log(arr)
  return arr;
}

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });