Exploring TypeScript, I encountered a puzzle where I needed to substitute specific characters in a string with other characters. Essentially, replacing A with T, T with A, C with G, and G with C.
The initial code snippet provided to me looked like this:
export class Kata {
static dnaStrand(dna: string) {
//your code here
}
}
The expected outcomes should be:
dnaStrand("ATTGC") // returns "TAACG"
dnaStrand("GTAT") // returns "CATA"
My initial approach involved using a for loop to traverse the string and generate a new string by replacing the specified characters. However, upon testing the code on jsfiddle, I encountered the error message: Unexpected token 'export'
. I'm seeking assistance in resolving this issue. I believed that iterating over the string and sequentially replacing the characters using the replace method was a potential solution. Is this the best course of action?
export class Kata {
static dnaStrand(dna: string) {
for (let i = 0; i < dna.length; i++){
dna = dna.replace('A','T').replace('T', 'A').replace('C', 'G').replace('G', 'C');
}
return dna;
}
}
Kata.dnaStrand("ATTGC");