Implement a DNA Pair Generator - Implement a DNA Pair Generator

Tell us what’s happening:

hello, I am having trouble figuring out how to iterate through the string and pushing each character into the new subarray without pushing the entire string.

Right now it is only pushing the very first character into each subarray

Your code so far

function pairElement(str) {
  let newArr = new Array();
  let finalArr = new Array();
  for (let i = 0; i < str.length; i++){
     newArr.unshift(str[i]);
     newArr.length = 1;
     finalArr.push(newArr);
  }
  
  console.log(finalArr);
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36

Challenge Information:

Implement a DNA Pair Generator - Implement a DNA Pair Generator

here you are pushing newArr, right? what do you want to push instead?

oh, wait, you found a thing that JS does

run your code with a tool like this Online JavaScript Compiler, Visual Debugger, and AI Tutor - Learn JavaScript programming by visualizing code, it shows the execution

if you see it you may understand better when I tell you that you are pushing the same array to finalArr multiple times, so being always the same array they will all be the same

could you give me a hint? Im completely lost

actually I think I got it to push them into new sub arrays correctly:


function pairElement(str) {

  let newArr = [];

for (let i = 0; i < str.length; i++){

     newArr.push([str[i]])

}

return newArr;

}




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



finally solved this by myself! Thanks for the help

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.