Intermediate Algorithm Scripting - DNA Pairing

Tell us what’s happening:
My code made of bunch of if statements works, but is there any good reason to use switch statement here?
What exactly could I gain from using switch?
Is there somehow less room for error?

Solution states: “Using if statements would take too much code.”
In this case solution using switch has 391 characters of code whereas my code has 394 characters.
I get it that if there would be more cases the difference would be bigger but I also have a gut feeling that switch isn’t usually used for very large sets of data but for a couple of different cases.

Your code so far

function pairElement(str) {
  let arr = [];

  for (let i = 0; i < str.length; i++) {
    arr.push([str[i]])
  }
  
  for (let x = 0; x < arr.length; x++) {
    if (arr[x] == "A") {
      arr[x].push("T")
    } else if (arr[x] == "T") {
      arr[x].push("A")
    } else if (arr[x] == "C") {
      arr[x].push("G")
    } else if (arr[x] == "G") {
      arr[x].push("C")
    }
  }
  return arr
}
  

pairElement("ATCGA")

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - DNA Pairing

Link to the challenge:

I would use a switch or lookup object here because you are matching one value against your possibilities.

‘too much space’ is a bad explanation, imho

1 Like

So you mean you would like to have one clear object or lookup table to easily manage different kind of cases than rummaging through if-statements looking for the one particular case to change/fix.

My order of preference would be

  1. Lookup object

  2. Switch

  3. If-else

1 Like