Skip to content

Instantly share code, notes, and snippets.

@Git-I985
Created March 4, 2023 23:34
Show Gist options
  • Save Git-I985/4b89a0a3aae12f685b304a3f61119efa to your computer and use it in GitHub Desktop.
Save Git-I985/4b89a0a3aae12f685b304a3f61119efa to your computer and use it in GitHub Desktop.
DNA to RNA
// @ts-check
/* eslint no-restricted-syntax: ["off", "ForOfStatement"] */
// BEGIN (write your solution here)
const map = {
G: "C",
C: "G",
T: "A",
A: "U",
}
export default (dna) => {
const dnaNucleotids = dna.split('')
let result = ''
for (const dnaNucleotid of dnaNucleotids) {
const rnaNucleotid = map[dnaNucleotid]
if(!rnaNucleotid) {
result = null
break
} else {
result += rnaNucleotid
}
}
return result
}
// OR
export default (dna) => {
const dnaNucleotids = dna.split('')
const known = (dnaNucleotid) => Object.hasOwn(map, dnaNucleotid)
const dnaToRnaMapper = (dnaNucleotid) => map[dnaNucleotid]
return dnaNucleotids.every(known) ? dnaNucleotids.map(dnaToRnaMapper).join('') : null
}
// END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment