Reputation: 13
I am working on an exercise by exercism called "Protein Translation". You can see the instructions here https://exercism.io/my/solutions/c7e4d84bad424a5e80f5a86d1242c923.
My logic:
I created an array (codonArr) that took in an rna sequence and split the values into 3 characters (using for loop). i.e (AUGUCC) would be ["AUG", "UCC"]
I created a second array (proteinArr) to store the matching codons and convert them to their protein name (using for loop to loop through codonArr and then using an if else to match codons to proteins). i.e codonArr [AUG, UUG] would be used to create proteinArr ["Methionine", "Leucine"]
However, I am not getting the results I'm expected.
Code: Code snippet
Results: Results snippet
Upvotes: 1
Views: 46
Reputation: 128
The error is in the logic of the if statement you use. When an if statement contains a logic operation like 'AND', 'OR', 'NOT', all parts before and after those operators will be evaluated and, in Javascript, will result in either truthy or falsy.
(see mdn:truthy and mdn:falsy what that means in detail).
codonArr[i] === 'UUU' will only be true when the value of the variable codonArr[i] is equal to 'UUU', but the static string 'UUC' is always true. In effect your elseif statement has these outcomes:
if(true || true) and if(false || true) which will always evaluate in if(true).
Upvotes: 1