DNA Pairing
1. Requirements
- The DNA Strand lacks a paired base. Based on each base, the matching base is found, and the result is returned as a second array.
- Base pairs (base pair) is a pair of at and CG that matches the missing base for a given letter.
- The letters and the letters to which they are paired are inside an array, and then all the arrays are organized and encapsulated into an array.
2. Ideas
- Use. Split (") to divide the entered letter string into an alphabetic array
- Define the result array variable, traverse each given letter in the for loop, push to the two-dimensional element of the result array, use the switch statement to determine the base of each letter pair, push to the corresponding array
- Returns an array of results
3. Code
function pair(str) {var result=[];var temp = str.split(‘‘);for(var i=0;i<temp.length;i++){ result[i]=[]; result[i].push(temp[i]); switch(temp[i]){ case ‘A‘: result[i].push(‘T‘);break; case ‘T‘: result[i].push(‘A‘);break; case ‘G‘: result[i].push(‘C‘);break; case ‘C‘: result[i].push(‘G‘);break; }}return result;}pair("GCG");
4. RELATED LINKS
- Https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/push
- Https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/split
- Http://en.wikipedia.org/wiki/Base_pair
DNA Pairing-freecodecamp Algorithm Topics