0

I need to run conditions for each of the 4 letters, then store the opposite of it in the mRNA variable. A & T and C & G. I can't figure out how to run multiple if statements at the same time and do a separate thing for each one.

function findAcids() {
  //Converts all lowercase letters to uppercase
  var input = document.getElementById("inputBox").value
  var abc = ["B", "D", "E", "F", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S","U", "V", "W", "X", "Y", "Z"]
  input  = input.toUpperCase()
  document.getElementById("inputBox").value = input
  //Returning errors if user input isn't A, C, G, or T
  if (input.indexOf('A') > -1 || input.indexOf('T') > -1 || input.indexOf('C') > -1 || input.indexOf('G') > -1 ){
    console.log("No errors occurred")
  }
  if (input.search(/[^a-zA-Z]+/) != -1) {
    console.log("Didn't have A, T, C, G")
    return 1;
  }
  for (var i =0; i < abc.length; i++) {
    if(input.indexOf(abc[i]) > -1) {
      console.log("Didn't have A, T, C, G")
      return 1;
    }
  }
  //Convert A, T, C, G to mRNA
  var mRNA = ""
  for (var x = 0; x < input.length; x++) {
    if (input.includes('A') == true || input.includes('C') == true || input.includes('G') == true || input.includes('T') == true) {
      mRNA += 
    }
    else {
      console.log("Error: unknown error occurred while finding mRNA")
      return 1;
    }
  }
  document.getElementById("mRNA").innerHTML = mRNA
}

I tried to run a if statement for each letter, and if that if statement wasn't true, move on to the next one, however, this results in the first letter that becomes true to only print that for the other letters.

Mippy
  • 13
  • 3

1 Answers1

0

You can simplify this with a regular expression test.

const mapping = { 'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C' }; // pairs

function findAcids() {
  const nucleotides = document.querySelector('#inputBox').value
    .trim()
    .toUpperCase()
    .replace(/\W/g, '');
  
  const isValid = /^[ACGT]+$/.test(nucleotides);
  
  if (!isValid) {
    alert('Only A, T, C, G allowed');
    document.querySelector("#mRNA").value = '';
    return;
  }
  
  const mRNA = nucleotides.split('').map(nucleotide => mapping[nucleotide]).join('');
  document.querySelector("#mRNA").value = mRNA;
}
<input id="inputBox" value="ATCGTAGGTTTAGC" placeholder="Nucleotide sequence... " />
<button onclick="findAcids()">Find Acids →</button>
<input id="mRNA" placeholder="mRNA sequence..." />

Functional aproach

Here is a more function example that groups acids in triplets. It also contains many functions that explain what is happening along the way. This code is self-documenting, because you know what is happening every step of the way.

const __inputEl = document.querySelector('#inputBox');
const __outputEl = document.querySelector("#mRNA");

const nucleotidePairs = new Map([
  ['A', 'T'],
  ['T', 'A'],
  ['C', 'G'],
  ['G', 'C']
]);

// Transcribe RNA into mRNA and group by triplets
const findAcids = () => {
  const rna = rnaInputSanitize(fetchInput());
  if (rnaIsValid(rna)) {
    handleSuccess(rna); 
  } else {
    handleFailure();
  }
};

const handleSuccess = (rna) => {
  updateOutput(strGroup(rnaTranscribe(rna), 3, ' '));
};

const handleFailure = () => {
  showError();
  updateOutput('');
};

const rnaInputSanitize = (input) =>
  input.trim()
    .toUpperCase()
    .replace(/\W/g, '');

const rnaIsValid = (rna) => /^[ACGT]+$/.test(rna);

const rnaTranscribe = (rna) =>
  rna.replace(/[ACGT]/g, (nucleotide) =>
    nucleotidePairs.get(nucleotide));

const showError = () => {
  alert('Only A, T, C, G allowed');
};

const fetchInput = () => {
  return __inputEl.value;
};

const updateOutput = (value) => {
  __outputEl.value = value;
};

// https://stackoverflow.com/a/53896618/1762224
const strGroup = (str, n, delim = ' ') =>
  str.replace(new RegExp(`(?<=^(?:.{${n}})+)(?!$)`, 'g'), delim);

/* @unused */
const strGroupArr = (str, n) =>
  str.split(new RegExp(`(?<=^(?:.{${n}})+)(?!$)`));
<input id="inputBox" value="ATCGTAGGTTTAGCA" placeholder="Nucleotide sequence... " />
<button onclick="findAcids()">Find Acids →</button>
<input id="mRNA" placeholder="mRNA sequence..." />
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132