-2

How may I get the truthy values? I am trying to compare the values, but before i am comparing the keys. I want to exclude the keys which have falsy values.

function truthCheck(collection, pre) {
     let a=[]
   for(let e in collection){

   if(console.log(collection[e] && collection[e][pre]==pre )){

                 return true;   
   }else{
           return false;
   }

   }

}




truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true}, {"name": "FastForward", "onBoat": null}], "onBoat");
  • [`Object.keys(entriesBasePairs).forEach()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys) – Phil Apr 30 '20 at 00:48
  • 1
    For anyone who just posts a solution to the problem: [his post yesterday, of the same problem](https://stackoverflow.com/questions/61470944/when-i-access-to-a-key-i-want-its-value-and-set-it-in-an-array-the-access-is-b), and the website has free and open access to multiple solutions, official and user submitted. – ASDFGerte Apr 30 '20 at 00:48

2 Answers2

0

The code should look something like:

function pairElement(str){
  const s = str.split(''), a = [];
  s.forEach(v=>{
    switch(v.toUpperCase()){
      case 'C':
        a.push(['C', 'G']);
        break;
      case 'G':
        a.push(['G', 'C']);
        break;
      case 'A':
        a.push(['A', 'T']);
        break;
      case 'T':
        a.push(['T', 'A']);
        break;
    }
  });
  return a;
}
const cgc = pairElement('GCG'), ttgag = pairElement('ttgag');
console.log(cgc); console.log(ttgag);
StackSlave
  • 10,613
  • 2
  • 18
  • 35
-1

i did not know how to do it with forEach()

function pairElement(str) {
  let entriesBasePairs={

    A:"T",

    T:"A",

    C:"G",

    G:"C"

  }
let newArr=[];
let entriesBase =str.split("");
 //Object.entries(entriesBasePairs); 
for(let i=0;i<entriesBase.length;i++)

for (let [key, value] of Object.entries(entriesBasePairs)){

        if(entriesBase[i]==key){

             newArr.push([key, value])    
//console.log(`${key}: ${value}`);

}
}
console.log(newArr)
return newArr;
}
pairElement("GCG");