-1

Take each character, get its pair, and return the results as a 2d array.

Base pairs are a pair of AT and CG. Match the missing element to the provided character.

Return the provided character as the first element in each array.

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

This is my code, can someone tell me why its not working and how to fix?

function pairElement(str) {
  let newArr = [];
  let hiString = str.split(" ");

  for (let i = 0; i<hiString.length; i++) {
    if (i = "G") {
      newArr.push(["G", "C"]);
    }
    else if (i="C") {
      newArr.push(["C", "G"]);
    }
    else if (i="A") {
      newArr.push(["A", "T"]);
    }
    else if (i="T") {
      newArr.push(["T", "A"]);
    }
    return newArr
  }
}

console.log(pairElement("GCG"));
Reyno
  • 6,119
  • 18
  • 27
Big Man
  • 27
  • 2
  • `str.split(" ")` will only split on spaces. You don't have spaces in your input `"GCG"` so you're running the `for` loop with the whole input. Use `str.split("")` (empty string) instead. – D M Nov 24 '21 at 14:01
  • You need to use `==` or `===` inside your if statements for comparing, a single `=` is used for asignment. Furthermore you're comparing `i` which is a number against a string, this will always return `false`. You need to check the array at the specific index like `hiString[i] == 'G'` – Reyno Nov 24 '21 at 14:02
  • Also, you need to move your `return` statement outside the loop if you don't want to return after the first base you check. – D M Nov 24 '21 at 14:02
  • hi, i just done all these suggestions you guys made to me but the code is still not working. Any other suggestions? – Big Man Nov 24 '21 at 14:06

1 Answers1

0

First of all to split a string into separate characters you need to split by an empty string ("") not by space (" ").

Furthermore you're comparing a number (i) against a string. This will always fail. You need to use the item in your array at the specific index like hiString[i].

For comparing you need to use Equality operators not assignment operators. So = needs to be == or ===. The difference is explained in this SO post.

At last the return statement needs to be moved to outside the loop. Otherwise it will return after the first iteration meaning the rest of the loop never executes.

function pairElement(str) {
  let newArr = [];
  let hiString = str.split("");   // Changed " " to ""

  for (let i = 0; i<hiString.length; i++) {
    if (hiString[i] == "G") {     // Changed i = "G" to hiString[i] == "G"
      newArr.push(["G", "C"]);
    }
    else if (hiString[i]=="C") {  // Changed i = "C" to hiString[i] == "C"
      newArr.push(["C", "G"]);
    }
    else if (hiString[i]=="A") {  // Changed i = "A" to hiString[i] == "A"
      newArr.push(["A", "T"]);
    }
    else if (hiString[i]=="T") {  // Changed i = "T" to hiString[i] == "T"
      newArr.push(["T", "A"]);
    }
  }
  
  return newArr; // Moved out of the for loop
}

console.log(pairElement("GCG"));
Reyno
  • 6,119
  • 18
  • 27