DNA Pairing challenge with filter method

I was trying to solve the DNA Pairing challenge with the filter method. Also, my all outputs are correct. But, my solution was not accepted. What I am missing?

let arr = [];

function pairElement(str) {
  let test = str.split("");
  test.filter((element) => {
    arr.push(pairElementDna(element));
    return arr;
  });
  function pairElementDna(character) {
    let arr = [];

    if (character === "G") {
      arr.push(character, "C");
    }
    if (character === "C") {
      arr.push(character, "G");
    }
    if (character === "A") {
      arr.push(character, "T");
    }
    if (character === "T") {
      arr.push(character, "A");
    }
    return arr;
  }
  return arr;
}

console.log(pairElement("GCG"));

1 Like

filter is a method of arrays that creates a new array based on a callback, this callback should either return true or false and if it returns true it will add the element it is currently on to the new array, and in your case you are returning an array inside your callback which you should not do, but more importantly you are not even using the array created by filter, you should not push items into another array with filter instead you should say

const arr = "GCG".split().filter(e => e === 'G');
console.log(arr) //[G, G] there is no C as C is not equal to G 
//this is not the answer to your problem just an example of filter

//anything returned in the callback handed to filter will be 
//made into a boolean and returning array is the same as always
// returning true

with what you are trying to do you should use forEach not filter

1 Like

As was already explained, .filter takes a function that returns either true or false. You’re returning an array, which is a truthy value, so the filter doesn’t filter anything.

Also, it would really help to have a link to the challenge.

1 Like
1 Like

Thanks for the link. Let us know if you need more help beyond the hints that are already given.

1 Like

Why my solution is not accepting? Because same , outputs are there.

1 Like

Relying on global variables is fragile and prevents function reusability.

1 Like

But , returning as expected . Still , is not proper?

1 Like

Your function gets called multiple times by the test suite. What will happen when you call the function with a new string?

1 Like

I controlled but still looks working and not accepting by system. if you would to check I would appreciate .

1 Like

What’s your new code? I can’t help without seeing what you changed.

1 Like

I give up using the filter method to solve that challenge for now. Until someone to help me so now I am used with the map method. But, I am a little curious person so I wanted to know how can I solve with the filter method this challenge.

1 Like

I am more than happy to help you, but I can’t do much when you don’t share your code.

map is a more natural way to solve this challenge than filter. map is designed to make a new array from an old array. filter is designed to make a smaller array from an old array by filtering out elements.

1 Like

my code is :slight_smile:

let arr = [];

function pairElement(str) {
  let test = str.split("");
  test.filter((element) => {
    arr.push(pairElementDna(element));
    return arr;
  });
  function pairElementDna(character) {
    let arr = [];

    if (character === "G") {
      arr.push(character, "C");
    }
    if (character === "C") {
      arr.push(character, "G");
    }
    if (character === "A") {
      arr.push(character, "T");
    }
    if (character === "T") {
      arr.push(character, "A");
    }
    return arr;
  }
  return arr;
}

console.log(pairElement("GCG"));
1 Like

So the thing I don’t understand is why you are trying to use filter here. You are using the filter like a forEach. Why not use a forEach? The filter method is for removing things from an array.

1 Like

You can be right but even if I am using forEach method instead of the filter it is working and giving expected output as using the filter method but the system is not accepting my solution still. Really I am not understanding why not accepting my solution.

1 Like

As was already said, you’re using a global variable (the array you define at the very top), which is changed each time the function runs. When the function is called a second time, the array isn’t empty anymore. Try logging two function calls to see what I mean:

console.log(pairElement("ATCGA"));
console.log(pairElement("TTGAG"));
3 Likes

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2
3 Likes

I understood what I am missing. I used global variables instead of using local variables which the array I define at the top …

The below code is working well …

function pairElement(str) {
  let arr = [];

  let test = str.split("");
  test.filter((element) => {
    arr.push(pairElementDna(element));
    return arr;
  });
  function pairElementDna(character) {
    let arr = [];

    if (character === "G") {
      arr.push(character, "C");
    }
    if (character === "C") {
      arr.push(character, "G");
    }
    if (character === "A") {
      arr.push(character, "T");
    }
    if (character === "T") {
      arr.push(character, "A");
    }
    return arr;
  }
  return arr;
}

console.log(pairElement("ATCGA"));

Its good that you have the solution now, but there is another way that you might like as it will save from making all those if statements.

function pairElement(str) {
//Since we know that each value is a pair I can easily 
//save that information in this object
const dna = {G: 'C',  C: 'G',  A: "T",  T: "A",}

  //With that object made I can now  use map to say I want 
 //each item in the new array to be an array that holds the 
//old item as well as the corresponding item within the dna object
  return str.split("").map(e => [e, dna[e]]);
}

console.log(pairElement("GCG")) //[['G','C'], ['C', 'G'], ['G','C']]
1 Like