Code works but just want feedback on my code for DNA pairing challenge

function pairElement(str) {
  let base_pairs = {
    A:'T',
    T:'A',
    C:'G',
    G:'C'
  }
  let pairs = [];
  str.split("").map(letter =>{
    pairs.push([letter,base_pairs[letter]]);
  });
  return pairs;
}

pairElement("GCG");

Remember that map() returns an array containing what your callback returns for each element, your use of it is not ideal. The way you are using it is more appropriate for a forEach() or you need to make map return the correct thing

I suggest you try to use both methods appropriately just to make experience

@ilenia
Thanks for your feedback, great point, forgot map() actually returns an array.

so slight change to the code

function pairElement(str) {
  let base_pairs = {
    A:'T',
    T:'A',
    C:'G',
    G:'C'
  }

  let pairs = str.split("").map(letter =>{
    return [letter,base_pairs[letter]];
  });
  return pairs;
}
1 Like