こちらのサイト様のようなものを作りたいんですよね?
http://www.siyasui.ne.jp/android/uranai/ketueki/nanigatakei.htm
試しに血液型で10問作成してみました。
各血液型へのポイント配分については、適当にやりました。
ひとまず、ポイントが一番高いものだけ結果表示します。
本来は、2番目に高いものと比べるとのことで、
アルゴリズム思いつき次第、コード編集いたします。
html
1 < div id = " questionWrap " > </ div >
2 < button id = " check " > Check </ button >
3 < div id = " result " > </ div >
4
js
1 const $questionWrap = $ ( '#questionWrap' )
2 const $result = $ ( '#result' )
3
4 //declare CONST_VAL
5 const BLOOD_TYPE = {
6 A : 'A' ,
7 B : 'B' ,
8 O : 'O' ,
9 AB : 'AB' ,
10 }
11 const ANSWER = {
12 YES : 'yes' ,
13 NO : 'no' ,
14 }
15 const BLOOD_TYPE_IDX = {
16 YES : 1 ,
17 NO : 2 ,
18 }
19 const QUESTION_TEXT_IDX = 0 ;
20
21
22 const questionList = [ //[text, if yes, if no]
23 [ '自分は良く気配が出来る人だと思う。' , BLOOD_TYPE . A , BLOOD_TYPE . B ] ,
24 [ '自分は、神経がこまやかだと思う。' , BLOOD_TYPE . A , BLOOD_TYPE . O ] ,
25 [ '人の気持ちを敏感に感じとる。' , BLOOD_TYPE . AB , BLOOD_TYPE . O ] ,
26 [ '自分は思いやりがある方だと思う。' , BLOOD_TYPE . O , BLOOD_TYPE . B ] ,
27 [ '自分は、サービス精神旺盛だと思う。' , BLOOD_TYPE . A , BLOOD_TYPE . B ] ,
28 [ '自分は、争い事を避けるために自分を抑えることが多いと思う。' , BLOOD_TYPE . AB , BLOOD_TYPE . B ] ,
29 [ '自分は、慎重堅実で完全に納得しないと行動しないと思う。 ' , BLOOD_TYPE . A , BLOOD_TYPE . B ] ,
30 [ '自分は、どちらかと言うとハメをはずさない優等生タイプだと思う。' , BLOOD_TYPE . A , BLOOD_TYPE . B ] ,
31 [ '自分は、潔癖である。' , BLOOD_TYPE . A , BLOOD_TYPE . O ] ,
32 [ '自分は、きれい好きである。' , BLOOD_TYPE . A , BLOOD_TYPE . O ] ,
33 ]
34
35 const getQuestionDom = ( { questionId , questionNum , questionText } ) => {
36 let str = '' ;
37 str += '<div class="question">' ;
38 str += '<legend>' ;
39 str += ` <span>Q ${ questionNum } :</span> ` ;
40 str += ` <span> ${ questionText } </span> ` ;
41 str += '</legend>' ;
42 str += '<label>' ;
43 str += ` <input class="answer-radio" type="radio" name="q ${ questionNum } " value= ${ ANSWER . YES } data-question-id= ${ questionId } /> ` ;
44 str += ` <span> ${ ANSWER . YES } </span> ` ;
45 str += '</label>' ;
46 str += '<label>' ;
47 str += ` <input class="answer-radio" type="radio" name="q ${ questionNum } " value= ${ ANSWER . NO } data-question-id= ${ questionId } /> ` ;
48 str += ` <span> ${ ANSWER . NO } </span> ` ;
49 str += '</label>' ;
50 str += '</div>' ;
51 return str ;
52 }
53
54 const appendQuestionDom = ( ) => {
55 const appendDomStr = questionList . reduce ( ( domStr , question , idx ) => {
56 return domStr += getQuestionDom ( {
57 questionId : idx ,
58 questionNum : idx + 1 ,
59 questionText : question [ QUESTION_TEXT_IDX ]
60 } )
61 } , '' ) ;
62 $questionWrap . append ( appendDomStr )
63 }
64 appendQuestionDom ( ) ;
65 const $answerRadio = $ ( '.answer-radio' ) ;
66 const initPoint = 0
67 //init bloodTypeMap
68 const bloodTypeMap = new Map ( )
69 Object . values ( BLOOD_TYPE ) . forEach ( ( bloodType ) => {
70 bloodTypeMap . set ( bloodType , { bloodType , point : initPoint } )
71 } )
72 //init questionMap
73 const questionMap = new Map ( )
74 questionList . forEach ( ( question , idx ) => {
75 questionMap . set ( idx , {
76 text : questionList ,
77 [ ANSWER . YES ] : {
78 addDestination : question [ BLOOD_TYPE_IDX . YES ] ,
79 } ,
80 [ ANSWER . NO ] : {
81 addDestination : question [ BLOOD_TYPE_IDX . NO ] ,
82 } ,
83 } )
84 } )
85
86 const check = ( ) => {
87 var total = 0 ;
88 var result = '' ;
89 const addPoint = 1 ;
90
91 $answerRadio . filter ( ':checked' ) . map ( ( idx , element ) => {
92 const questionID = element . dataset . questionId ;
93 const question = questionMap . get ( parseInt ( questionID ) )
94 const answer = element . value ;
95 const addDestination = question [ answer ] . addDestination ;
96 const bloodType = bloodTypeMap . get ( addDestination ) ;
97 bloodType . point += addPoint ;
98 } ) ;
99
100 const sortedBloodTypePointsList = Array . from ( bloodTypeMap . values ( ) ) . sort ( ( a , b ) => {
101 return b . point - a . point ;
102 } )
103
104 $result . text ( ` 一番ポイントが高かった血液型は、 ${ sortedBloodTypePointsList [ 0 ] . bloodType } 型です。 ` )
105 }
106
107 $ ( '#check' ) . click ( check ) ;