In the following code, I want the generate Questions, Options, and Answers based on the list of options. I am successfully able to generate the Questions and Answers along with options. But still, sometimes options are getting repeated also when I am dealing with a large list making my work harder. I want a simple solution by which any large list should give me Questions, Options, and Answers.
Please note:
I want options must be random.
Options must have the answer option on a random position.
Options must be not repeated.
function myFunction() {
var myList=document.getElementById("myList").value;
var lines = myList.split("n"); //split lines
var addQ="";
var result1 = "";
var option2="";
lines.map(line => {
var result = line.split("s320/");
result1 += '"'+result[1] + 'n';
addQ+='new Question("Choose '+result[1]+' character from below!", ["", "", "", ""], "'+result[1]+'"),'+"n";
})
var addOption1 = result1.split("n");
for (var i = 0; i < addOption1.length; i++) {
addQ=addQ.replace('["", "", "", ""]','["", '+addOption1[0]+'", "", ""]')
.replace('["", "", "", ""]','["", "", "", '+addOption1[1]+'"]')
.replace('["", "", "", ""]','["",'+addOption1[2]+'", "", ""]')
.replace('["", "", "", ""]','["", "", "", '+addOption1[3]+'"]')
.replace('["", "", "", ""]','["","",'+addOption1[4]+'", "",]')
.replace('["", "", "", ""]','["", '+addOption1[5]+'", "", ""]');
addQ=addQ.replace('["", '+addOption1[0]+'", "", ""]','['+addOption1[3]+'", '+addOption1[0]+'", '+addOption1[5]+'", '+addOption1[4]+'"]')
.replace('["", "", "", '+addOption1[1]+'"]','['+addOption1[4]+'", '+addOption1[5]+'", '+addOption1[4]+'", '+addOption1[1]+'"]')
.replace('["",'+addOption1[2]+'", "", ""]','['+addOption1[3]+'",'+addOption1[2]+'", '+addOption1[5]+'", '+addOption1[4]+'"]')
.replace('["", "", "", '+addOption1[3]+'"]','['+addOption1[4]+'", '+addOption1[2]+'", '+addOption1[2]+'", '+addOption1[3]+'"]')
.replace('["","",'+addOption1[4]+'", "",]','['+addOption1[0]+'",'+addOption1[5]+'",'+addOption1[4]+'", '+addOption1[5]+'",]')
.replace('["", '+addOption1[1]+'", "", ""]','['+addOption1[1]+'", '+addOption1[5]+'", '+addOption1[3]+'", '+addOption1[5]+'"]');
}
document.getElementById("result").value= addQ;
}
<!DOCTYPE html>
<html>
<body onload="myFunction()">
<b><label for="fname">List of Options:</label></b><br><br>
<textarea autocomplete="off" cols="30" id="myList" name="message" rows="10" style="border: 3px solid #73AD21; width: 90%;">
s320/Apple.jpg
s320/Ball.jpg
s320/Cat.jpg
s320/Doll.jpg
s320/Egg.jpg
s320/Frog.jpg</textarea><br><br>
<b><label for="fname">Questions + Options + Answers:</label></b><br><br>
<textarea autocomplete="off" cols="30" id="result" name="message" rows="10" style="border: 3px solid #73AD21; width: 90%;"></textarea>
</body>
</html>
2
Answers
I got rid of the complex "replace" methods in the code and updated it to delete the option from the options list when added as option to string, thus ensuring a unique value for each random selection.
Here’s alternative answer
https://1389876.playcode.io/ (result) / https://playcode.io/1389876 (code)