Intermediate Algorithm Scripting: DNA Pairing. Question Why is this wrong

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

Can someone please explain me why my code is wrong ? I don´t understand it. The solution also works with the push method into an empty Array.

Thank you in advance!!!

// Convert str to Array
//
var emptyArray = [];
var a = ["A", "T"];
var c = ["C", "G"];
var g = ["G", "C"];
var t = ["T", "A"];
//emptyArray.push(g,a)
function pairElement(str) {
  var splitArray = str.split("");
  for(var i = 0; i < splitArray.length; i++) {
    if(splitArray[i] == "A" ) {
      emptyArray.push(a)
    }
    else if (splitArray[i] == "C") {
      emptyArray.push(c)
    
    } 
    else if (splitArray[i] == "G") {
      emptyArray.push(g)
    }
    else if (splitArray[i] == "T") {
      emptyArray.push(t)
    }
  }
  return emptyArray
}

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

After I moved your global variables into the function, all tests were passed. It might be a bug, since your code seemed correct and worked fine in chrome dev tools.

	function pairElement(str) {
    	var emptyArray = [];
        var a = ["A", "T"];
        var c = ["C", "G"];
        var g = ["G", "C"];
        var t = ["T", "A"];
		var splitArray = str.split("");
		for (let i = 0; i < splitArray.length; i++) {
			if (splitArray[i] == "A" ) {
				emptyArray.push(a)
			} else if (splitArray[i] == "C") {
				emptyArray.push(c)
			} else if (splitArray[i] == "G") {
			  emptyArray.push(g)
			} else if (splitArray[i] == "T") {
			  emptyArray.push(t)
			}
		}
		return emptyArray;
	}

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and not posting full working solutions.

Thank you for understanding.

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2
1 Like

Sorry, that was my mistake.

1 Like

Thank you two !!

Is there like a general rule that i should put variables that belong to my function into the function or is it more like depending ?

Generally speaking, you don’t want functions to have “side effects”, meaning that your function should not change values outside of their scope. What caused your problem is that the function relied on emptyArray being an empty array when the function was called, but it wasn’t. You could start your function by resetting emptyArray to [], but it’s a much better idea to just scope that variable to the function itself.

Hey, my code is similar to yours. In my console.log I get the “right” result, but I’m still not passing the tests!

function pairElement(str) {
  let result = []
  for (let i = 0; i < str.length; i++){
    switch(str[i]){
      case "C":
      result.push(["C" + "G"])
      break
      case "G":
      result.push(["G" + "C"])
      break
      case "A":
      result.push(["A" + "T"])
      break
      case "T":
      result.push(["T" + "A"])
      break
      
    }
  } 
  return result;
}

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

I think I’m missing something basic!

From the challenge description

For example, for the input "GCG",return [["G", "C"], ["C","G"],["G", "C"]]

Your code is instead returning [["GC"], ["CG"], ["GC"]]

+ is the concatenation Operator when you are using it with strings

2 Likes