Background: From the database, randomly generate a paper, the type of test paper is divided into radio, multi-choice, judging three types of questions.
First I need to generate a random number ID (before this I need to know the number of questions in the database, so that I can set the random number), and based on the generated random number ID, to find the corresponding topic. In the JS array operation, there are filter, splice, concat, every, find and so on. I need to get the specific data out of the database, and I'm going to return a set of objects, so I need to filter out a specific section.
The code is as follows:
var Danxuan = Getrandomnum (danxuan_count,20);
var Duoxuan = Getrandomnum (duoxuan_count,5);
var Panduan = Getrandomnum (panduan_count,10);
To save the generated random number to example,
var example = {Danxuan:danxuan.sort (sortnumber), Duoxuan:duoxuan.sort (Sortnumber), Panduan:panduan.sort (Sortnumber) }
Find a Database
Models. Answer.findall (). Then (function (Result) {
var danxuanlist = result.filter (t = t.exampletype== "single choice");//nodejs6.0 support
var duoxuanlist = result.filter (t = t.exampletype== "multi-choice");
var panduanlist = result.filter (t = = t.exampletype== "Judgment question");
New Object
var examplelist = {duoxuan:[],danxuan:[],panduan:[]};
Find the corresponding data in each list based on the randomly generated number ID.
Example.danxuan.forEach (function (i,v) {
ExampleList.danxuan.push (Danxuanlist[i]);
})
Example.duoxuan.forEach (function (i,v) {
ExampleList.duoxuan.push (Duoxuanlist[i]);
})
Example.panduan.forEach (function (i,v) {
ExampleList.panduan.push (Panduanlist[i]);
})
Res.json (examplelist);//test paper generation completed
});
Randomly generate a test paper, the type of test paper is divided into radio, multi-choice, judging three types of questions. nodejs6.0 MySQL