Enumeration Questionnaire type logic using javascript 枚举问卷类型逻辑使用JavaScript

huangapple go评论69阅读模式
英文:

Enumeration Questionnaire type logic using javascript

问题

I can provide the translation for the code part:

var correctList = ['afghanistan','albania','algeria','argentina','armenia','angola'];
var score = 0;
var yourAnswerAre = [];
document.querySelector('button').addEventListener('click', function () {
  document.querySelectorAll('.enumeration').forEach(input => {
    var countNum = 1;
    correctList.forEach(list => {
      if(input.value == list){
        score++;
        yourAnswerAre.push({
          'Numb': countNum,
          'Answer': input.value,
          'IsCorrect': 'yes'
        })
      }else{
        score--;
        yourAnswerAre.push({
          'Numb': countNum,
          'Answer': input.value,
          'IsCorrect': 'no'
        })
      }
      countNum++
    })
  })

  document.querySelector('span').innerText = score;
  console.log(yourAnswerAre)
})

I won't provide translations for the HTML part as you requested code-only translation.

英文:

Good day.
i'm trying to create enumeration type questionnaire, but i don't know the logic behind it. I want to know how to prevent this from counting as a correct. check the picture. Enumeration Questionnaire type logic using javascript
枚举问卷类型逻辑使用JavaScript

also, it is correct if it is matched in the array list even it is not in order or in sequence.

Here's what i have tried and it is really messy. sorry

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var correctList = [&#39;afghanistan&#39;,&#39;albania&#39;,&#39;algeria&#39;,&#39;argentina&#39;,&#39;armenia&#39;,&#39;angola&#39;]
var score = 0;
var yourAnswerAre = [];
document.querySelector(&#39;button&#39;).addEventListener(&#39;click&#39;, function () {
document.querySelectorAll(&#39;.enumeration&#39;).forEach(input =&gt; {
var countNum = 1;
  correctList.forEach(list =&gt; {
	 if(input.value == list){
     score++;
     yourAnswerAre.push({
     		&#39;Numb&#39;: countNum,
        &#39;Answer&#39;: input.value,
        &#39;IsCorrect&#39;: &#39;yes&#39;
     })
   }else{
     score--;
     yourAnswerAre.push({
     		&#39;Numb&#39;: countNum,
        &#39;Answer&#39;: input.value,
        &#39;IsCorrect&#39;: &#39;no&#39;
     })
   }
   countNum++
  })
})

document.querySelector(&#39;span&#39;).innerText = score;
console.log(yourAnswerAre)
})

<!-- language: lang-html -->

&lt;label&gt;score: &lt;span&gt;0&lt;/span&gt;&lt;/label&gt;
&lt;h3&gt;
Give atleast 5 Country starts with letter A
&lt;/h3&gt;
&lt;input type=&quot;text&quot; class=&quot;enumeration&quot;/&gt;
&lt;input type=&quot;text&quot; class=&quot;enumeration&quot;/&gt;
&lt;input type=&quot;text&quot; class=&quot;enumeration&quot;/&gt;
&lt;input type=&quot;text&quot; class=&quot;enumeration&quot;/&gt;
&lt;input type=&quot;text&quot; class=&quot;enumeration&quot;/&gt;
&lt;button&gt;
save
&lt;/button&gt;

<!-- end snippet -->

答案1

得分: 1

你在对输入进行所有可能的答案匹配。我移除了一个 foreach 并使用了 includes

为了防止重复输入,我创建了一个 used 数组,当一个答案已经给出后,将其设置为 true。

if (countries.includes(answer) && !used[countries.indexOf(answer)]) {
    used[countries.indexOf(answer)] = true;
    // ...
}

我还使用 insertAdjacentHTML 添加了一个标记方案。

input.insertAdjacentHTML("afterend", '<span style="color:red">&#10006</span>');

HTML 部分不需要翻译。

英文:

You were running through all of the possible answers against the inputs. I removed a foreach and used includes instead.

To guard against repeated inputs, I created the used array, which is set to true after an answer has been given.

 if (countries.includes(answer) &amp;&amp; !used[countries.indexOf(answer)])
     {
     used[countries.indexOf(answer)]=true;
     ...

I added a martkung scheme with insertAdjacentHTML

   input.insertAdjacentHTML(&quot;afterend&quot;, &#39;&lt;span style=&quot;color:red&quot;&gt;&amp;#10006&lt;/span&gt;&#39;);

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-js -->

var countries = [&#39;afghanistan&#39;, &#39;albania&#39;, &#39;algeria&#39;, &#39;angola&#39;, &#39;argentina&#39;, &#39;armenia&#39;];
var used = new Array(countries.length).fill(false);

var score = 0;
var yourAnswers = [];
var countNum = 1;

document.querySelector(&#39;button&#39;).addEventListener(&#39;click&#39;, function () {
document.querySelectorAll(&#39;.enumeration&#39;).forEach(input =&gt; {
  var answer = input.value.toLowerCase();

  if (countries.includes(answer) &amp;&amp; !used[countries.indexOf(answer)])
     {
     used[countries.indexOf(answer)]=true;
     score++;
     yourAnswers.push({
        &#39;Number&#39;: countNum,
        &#39;Answer&#39;: input.value,
        &#39;Correct&#39;: &#39;yes&#39;
     });
     input.insertAdjacentHTML(&quot;afterend&quot;, &#39;&lt;span style=&quot;color:green&quot;&gt;&amp;#10003&lt;/span&gt;&#39;);
   } else {
     yourAnswers.push({
        &#39;Number&#39;: countNum,
        &#39;Answer&#39;: input.value,
        &#39;Correct&#39;: &#39;no&#39;
     });
     input.insertAdjacentHTML(&quot;afterend&quot;, &#39;&lt;span style=&quot;color:red&quot;&gt;&amp;#10006&lt;/span&gt;&#39;);
   }
   countNum++;
  });

document.querySelector(&#39;label&gt;span&#39;).innerText = score;

})

<!-- language: lang-html -->

&lt;h3&gt;Give 5 countries that start with the letter A&lt;/h3&gt;
&lt;input type=&quot;text&quot; class=&quot;enumeration&quot;&gt;&lt;br&gt;
&lt;input type=&quot;text&quot; class=&quot;enumeration&quot;&gt;&lt;br&gt;
&lt;input type=&quot;text&quot; class=&quot;enumeration&quot;&gt;&lt;br&gt;
&lt;input type=&quot;text&quot; class=&quot;enumeration&quot;&gt;&lt;br&gt;
&lt;input type=&quot;text&quot; class=&quot;enumeration&quot;&gt;&lt;br&gt;
&lt;button&gt;mark&lt;/button&gt;&lt;label&gt;score: &lt;span&gt;0&lt;/span&gt;&lt;/label&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月13日 16:38:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定