1

Doing the DNA challenge and so close but clearly misunderstanding prototype.split(""). What's the best way to turn these strings ["AC", "CA", "TA"] into subarrays? [["A","C"]["C","A"]["T","A"]]

function pairElement(str) {
  //break into array
  var arr = str.split("");

  //add new letter (could be refactored as switch)
  for (i = 0; i < arr.length; i++) {
    if (arr[i] == "G") {
      arr[i] += "C";
    } else if (arr[i] == "C") {
      arr[i] += "G";
    } else if (arr[i] == "T") {
      arr[i] += "A";
    } else if (arr[i] == "A") {
      arr[i] += "T";
    }
  }

  //break into arrays again
  //this is how I'm trying to use.split to break it up. Doesn't work.
  var broken = [];
  for (x = 0; x < arr.length; x++) {
    broken += arr[x].split("");
  }

  //return
  return arr;
}

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

5 Answers 5

3

you can use .map and split them by ""

var o =  ["AC", "CA", "TA"];

var s =  o.map(e=> e.split(""));

console.log(s)

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

Comments

2

You actually have to just push the split result in the broken array and return it !

function pairElement(str) {
  //break into array
  var arr = str.split("");

  //add new letter (could be refactored as switch)
  for (i = 0; i < arr.length; i++) {
    if (arr[i] == "G") {
      arr[i] += "C";
    } else if (arr[i] == "C") {
      arr[i] += "G";
    } else if (arr[i] == "T") {
      arr[i] += "A";
    } else if (arr[i] == "A") {
      arr[i] += "T";
    }
  }

  //break into arrays again
  //this is how I'm trying to use.split to break it up. Doesn't work.
  var broken = [];
  for (x = 0; x < arr.length; x++) {
    broken.push(arr[x].split(""));
  }

  //return
  return broken;
}

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

2 Comments

This may not be the neatest syntax but you get the answer because you're totally right yaaaaay! Thank you!
Well the point is for you to understand where the problem is ! and from there you can optimize your code!
1

To answer your 'what's the best way' question, map your arrays into the split versions of themselves:

const subarrays = array.map(pair => pair.split());

Comments

1

Very simple in functional style:

> seq = ['AC', 'CA', 'TA']
[ 'AC', 'CA', 'TA' ]
> seq.map(s => s.split(''))
[ [ 'A', 'C' ], [ 'C', 'A' ], [ 'T', 'A' ] ]

1 Comment

Good to see a clean way of doing this. Thank you!
1

Overall, I'd do some refactoring on the whole function:

var m = new Map([["G", "C"], ["C", "G"], ["A", "T"], ["T", "A"]]);

function pairElement(str) {
  return [...str].map(c => [c, m.get(c)]);
}

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


And if there's a guarantee that the sub-arrays are never mutated, then you can save a good bit of memory by reusing arrays instead of creating them over and over.

var m = new Map([["G", ["G", "C"]], ["C", ["C", "G"]], ["A", ["A", "T"]], ["T", ["T", "A"]]]);

function pairElement(str) {
  return [...str].map(c => m.get(c));
}

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


But to directly answer your question, you can do it without explicit .split() calls. Since you know there's always two characters, you can use parameter destructuring on the strings.

var arr = ["AC", "CA", "TA"];

var s = arr.map(([a, b]) => [a, b]);

console.log(s)


Or even a little shorter using rest syntax, like this:

var arr = ["AC", "CA", "TA"];

var s = arr.map(([...a]) => a);

console.log(s)


Or using spread syntax in an array literal:

var arr = ["AC", "CA", "TA"];

var s = arr.map(s => [...s]);

console.log(s)

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.