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: