DEV Community

Randy Rivera
Randy Rivera

Posted on • Edited on

Algorithms and Scripting: Problems and Notes Part 2:

Currently a Monday, time is 6:00 PM I'm feeling pretty tired but I must continue this habit of learning and studying if I wanna be someone for my family and myself.

  • Anyways Let's continue, this challenge in particular is one where you have to take each character, get its pair and return the results in a 2nd array.
  • The base pairs are a pair of AT and CG. Match the missing element to the provided character and return the character as the first element in each array.
  • Basically for ATCGA return [["A", "T"], ["T","A"], ["C", "G"],["G","C"],["A","T"]]

  • Problem:

function pair(str) {

}

pairElement("GCG");
Enter fullscreen mode Exit fullscreen mode
  • Answer:
function pair(str) {
let letters = str.split("")

function singleLetter(initial) {
if (initial === "G") {
  return "C"
} else if (initial === "C") {
  return "G"
} else if (initial === "A") {
  return "T"
} else if (initial=== "T") {
  return "A";
 }
}


return letters.map(letter => {
  let result = [letter, singleLetter(letter)]
  return result;
})



}

console.log(pairElement("ATCGA")); will display [["A", "T"], ["T","A"], ["C", "G"],["G","C"],["A","T"]] 
Enter fullscreen mode Exit fullscreen mode

Again this doesn't have to be this complicated, there's other ways to solve this problem. Like, defining an object with all pair possibilities, which allows us to easily find by key or value.

What's the missing letter?

  • What we have to do here is find the missing letter in the passed letter range and return it.
  • If all letters are there then just return undefined.
  • Problem:
function fearNotLetter(str) {
  return str;
}

fearNotLetter("abce");
Enter fullscreen mode Exit fullscreen mode
  • Answer:
function fearNotLetter(str) {
  let alphabet = "abcdefghijklmnopqrstuvwxyz";
  let startAt = alphabet.indexOf(str[0]); 
  let letters = alphabet.slice(startAt); 

  for (let i = 0; i < str.length; i++) {
    if (letters[i] !== str[i]) { // <-- basically checks if (s !== s), (t !== t), (u !== v) etc.
      return letters[i];
    }
  }
  return undefined;
}

console.log(fearNotLetter("stvwx")); will display u
Enter fullscreen mode Exit fullscreen mode

Top comments (0)