Today I (tried) created some code to create mcq questions.
The code is supposed to generate a random codon, find its amino acid from the codon table/chart and display it on the document along with other (3) random wrong options.
I want to make a mcq with 4 options (1 is correct rest are wrong).
What I am trying to do below is: The computer will form a random sequence of 3 nucleotides (i.e.digits) using form() function. Variable formed
will store a codon (eg. UCA, ACC etc.) which will be the question.
Now I declared array arr
which will store the correct answer at 0th position.
Then I created a function generateWrongOptions() which will (is supposed to) add the other 3 dissimilar wrong answers to the array. What I tried to do here is that the function will declare a new amino acid (eg. Phe, Ile, Met etc.) which is stored as wrong
and a new empty array arr2
. The next loop is supposed to check if wrong
is already present in arr
or not; if it is not then it will push an element 'a' ('a' here doesn't has any meaning) in arr2, if it is then it won't. Now if
will check if the arr
length is equal to arr2
which simply means if the variable wrong
is unique or not (or is duplicate).
I wanted to create 4 options (1 was already present) hence I looped the code for i<3 times.
I found better ways to do this same task online, but those were more advanced and I couldn't understand them. Hence I'd come with my own solution (best I could've guessed).
const obj = {
UUU:"Phe",
UUC:"Phe",
UUA:"Leu",
UUG:"Leu",
CUU:"Leu",
CUC:"Leu",
CUA:"Leu",
CUG:"Leu",
AUU:"Ile",
AUC:"Ile",
AUA:"Ile",
AUG:"Met",
GUU:"Val",
GUC:"Val",
GUA:"Val",
GUG:"Val",
/* - */
UCU:"Ser",
UCC:"Ser",
UCA:"Ser",
UCG:"Ser",
CCU:"Pro",
CCC:"Pro",
CCA:"Pro",
CCG:"Pro",
ACU:"Thr",
ACC:"Thr",
ACA:"Thr",
ACG:"Thr",
GCU:"Ala",
GCC:"Ala",
GCA:"Ala",
GCG:"Ala",
/* - */
UAU:"Tyr",
UAC:"Tyr",
UAA:"Stop",
UAG:"Stop",
CAU:"His",
CAC:"His",
CAA:"Gln",
CAG:"Gln",
AAU:"Asn",
AAC:"Asn",
AAA:"Lys",
AAG:"Lys",
GAU:"Asp",
GAC:"Asp",
GAA:"Glu",
GAG:"Glu",
/* - */
UGU:"Cys",
UGC:"Cys",
UGA:"Stop",
UGG:"trp",
CGU:"Arg",
CGC:"Arg",
CGA:"Arg",
CGG:"Arg",
AGU:"Ser",
AGC:"Ser",
AGA:"Arg",
AGG:"Arg",
GGU:"Gly",
GGC:"Gly",
GGA:"Gly",
GGG:"Gly",
};
const digit = ['U', 'C', 'A', 'G'];
function x() {
return Math.floor(Math.random()*4);
};
function form() {
return digit[x()] digit[x()] digit[x()]
}
let formed = form();
let arr = [obj[formed]];
function generateWrongOptions() {
for (i = 0; i < 4; i ) {
let wrong = obj[form()];
let arr2 = [];
for (i = 0; i < arr.length; i ) {
if (wrong!==arr[i]){
arr2.push('a');
};
if(arr2.length==arr.length){
arr.push(wrong)
}
else {
generateWrongOptions()
};
};
};
};
generateWrongOptions();
for (let n of arr) {
console.log(n)
}
Console returns Maximum calls exceeded;
On the other hand a similar code I wrote before creating this - as a guideline - to form an array of 4 different numbers works:
function x() {
return Math.floor(Math.random()*10)
}
let y = x();
let arr = [y];
function aa() {
for (i = 0; i < 4; i ) {
let z = x()
let arr2 = []
for (i = 0; i < arr.length ; i )
{
if (z!==arr[i])
{arr2.push('a')};
}
if (arr2.length==arr.length)
{arr.push(z)}
else {aa()}
};
};
aa();
console.log(arr);
I think I can fix this code by declaring a new array of all the amino acids in the codon table (obj), but still want to know why the first code doesn't work while the latter does.
CodePudding user response:
I'm not so sure if I understand your code correctly. However, I can see that you have two for loops in which you forgot to create a new variable:
you used "for (i .... )
" , and you forgot to say "for (let i ..... )
". Another issue i noticed is the redeclaration of "arr" in the last function, which I found weird since you already declared it outside of the function scope. In addition, there is an "arr2" that was also not declared with "let" or "var" words.