1

How can I change every specific value within an array to another specific value? My function splits a string into an array, and if a specific index == "T" it's supposed to change that value to "U", join the values in the array, and return the result. But the value "T" isn't changing within the array as I loop through it. What am I doing wrong?

function DNAtoRNA(dna) {

let array = dna.split();
let rna = "";

for(var i = 0; i < array.length; i++) {
  if(array[i] == 'T') {
    a[i] = 'U';
  }
  }
rna = array.join();
return rna;
}

Example input = "ATFT", output = "AUFU"

ianw
  • 19
  • 4
  • 1
    You're using `a[i] = 'U';` instead of `array[i] = 'U';` – Stephen Gilboy Jun 29 '22 at 21:01
  • Although there are better solutions to do that, your problem is **a[i]** should be **array[i]** – imvain2 Jun 29 '22 at 21:01
  • 1
    The real reason this doesn't work is because you are using `dna.split()`, and using `.split()` without a parameter will simply wrap the original string into an array. So your `array` contains `["ATFT"]`, whose only element isn't equal to `'T'`. – Ivar Jun 29 '22 at 21:04

0 Answers0