1

I'm trying to get the console.log output at the bottom of the sample code. Currently, my code is returning undefined. I would like to know which part I did wrong.

I would also like to know if there are other, more efficient approaches in solving the problem.

function pairElement(str) {

  return str.split('')
    .forEach(function(element){
      return element.split('').map(function(pair){
        if (pair == "G") {return ["G","C"];}
        else if (pair == "C") {return ["C","G"];}
        else if (pair == "T") {return ["T","A"];}
        else {return ["A","T"];}
      });
    });
}

console.log(pairElement("ATCG"));
// -> Should be [["A","T"], ["T","A"], ["C","G"], ["G","C"]]
// -> But currently, it is returning undefined
2
  • 2
    Why are you splitting it twice? Commented Jul 30, 2016 at 22:39
  • I think the first split makes the "ATCG" input into ["ATCG"]. The second split then makes it into [["A"], ["T"], ["C"], ["G"]], creating the 2-layers deep array that I need. Then I think need to push or return based on the index of the layer-1 array. Commented Jul 30, 2016 at 22:45

4 Answers 4

2

You don't need to do .split twice. Here's a possible solution:

function pairElement(input){
    var pairs = { A: "T", T: "A", C: "G", G: "C" };
    return input.split("").map(char => [char, pairs[char]]);  // ES6 arrow function
}

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

Fiddle


"ATCG".split("") turns the string into an array ["A", "T", "C", "G"]. Then by applying a mapping function we can transform the output, character by character, into predefined pairs [["A","T"], ["T","A"], ["C","G"], ["G","C"]].

Sign up to request clarification or add additional context in comments.

2 Comments

Accepting this as the correct answer since it demonstrates how key/value pairs can be used instead of if/else statements. Thanks. I learned a new technique because of this.
Trivial optimization: Since it could be static, move the pairs var outside the function so you're not creating it every time the function is called.
1

What's returned is str.split('').forEach(...). The forEach iterator method does not return anything; it simply runs for every item in the split array.

Perhaps you meant to .map(...) instead? This would take the returned values from the callback function and create a new array with them.

Comments

1

Use .map instead .forEach. The .forEach() executes the callback function once for each array element; unlike map() or reduce() it always returns the value undefined and is not chainable. The typical use case is to execute side effects at the end of a chain.

Comments

1

You're splitting too many times and you don't need the foreach. The split creates an array of from the string, (i.e. ["A","T","C","G"]). The map then creates a new array with each element modified according to your function (i.e. "A" becomes ["A","T"])

function pairElement(str) {
    return str.split('').map(function(element){
        if (element == "G") {return ["G","C"];}
        else if (element == "C") {return ["C","G"];}
        else if (element == "T") {return ["T","A"];}
        else {return ["A","T"];}
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.