Intermediate Algorithm Scripting: DNA Pairing Help

Can someone help explain why I can’t pass the test? I checked the console and the answer is exactly right as stated in the requirements.

function pairElement(str) {

let store = [];

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

if (str[i] == "T" ) {

let word = str[i].split("").concat("A").join().split(" ")
store.push(word)
}


else if (str[i] == "A" ) {

let word = str[i].split("").concat("T").join().split(" ")
store.push(word)
}

else if (str[i] == "G" ) {

let word = str[i].split("").concat("C").join().split(" ")
store.push(word)
}

else if (str[i] == "C" ) {

let word = str[i].split("").concat("G").join().split(" ")
store.push(word)
}

}


  return store;
}

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

My console answer :

link to challenge : https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/dna-pairing

It’s asking you to return eg ["T", "A"], an array with two elements, whereas you’re returning eg ["T,A"], an array with one element.

Also, this:

if (str[i] == "T") {
  let word = str[i].split("").concat("A").join().split(" ");
  store.push(word);

Is a very complicated way of saying

if (str[i] == "T") {
  store.push(["T,A"]);

Which has the added benefit of making it immediately obvious where the error is without having to run the code

2 Likes

Oh thanks man lol how didn’t i realize that…fixed my code. works now! :slight_smile: as for the obvious approach yeah i guess that works too but i didn’t realize i could do that, its like im just so systematic on the buit in methods

function pairElement(str) {

let store = [];

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

if (str[i] == "T" ) {

let word = str[i].split("").concat("A")
store.push(word)
}


else if (str[i] == "A" ) {

let word = str[i].split("").concat("T")
store.push(word)
}

else if (str[i] == "G" ) {

let word = str[i].split("").concat("C")
store.push(word)
}

else if (str[i] == "C" ) {

let word = str[i].split("").concat("G")
store.push(word)
}

}


  return store;
}

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

You could just do word = [str[i]]
Or just put everything in the push method, you don’t even need to use variables here - you already check what str[i] is so it can’t be something else

appreciate the feedback. but im just not that flexible lol my brain ain’t that smart realize everything

It’s not about being smart per se, it just takes time to learn things. What you’ve done is based on what applying what you’ve learned: how you’ve done it (the line of code you’ve used to build the word value) is clever, it’s just that it’s unnecessarily clever.

A description of the ideal path programmers go through that I like is something like you start off knowing nothing, and the code is very basic, you don’t know enough to leverage many language features or patterns. Then you learn some stuff, and the code becomes clever. Then you get more experience and the code goes back to being very basic and often stupidly simple.

For example:

function pairElement(str) {
  const pairs = [];

  for (let i = 0; i < str.length; i++) {
    switch (str[i]) {
      case "T":
        pairs.push(["T", "A"]);
        break;
      case "A":
        pairs.push(["A", "T"]);
        break;
      // and so on
    }

Yeah i guess… i get overwhelmed but after solving it with assistance and somehow looking back, its much simpler than i think. i just can’t think of proper way to solve a problem in simpler way which is tough. learning programming makes me feel dumb as hell

You don’t learn anything if you don’t have difficulties, and those that actually learn are those that keep trying when confronting those difficulties instead of surrendering

Do you think its okay for me to watch fcc challenge videos on youtube for assistance? like i don’t look at the answer immediately but i just watch like the beginning for some advice and base understanding then try to figure the rest of the code out myself.

Because right now im stuck on “Intermediate Algorithm Scripting: Missing letters” and i looked at the fcc 3 hints and i still don’t know how to go at it.

link to challenge: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/missing-letters/

You can do whatever you feel is right for you, but you should try to solve the challenge yourself

i tried man and i feel lost