英文:
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.
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 = ['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)
})
<!-- language: lang-html -->
<label>score: <span>0</span></label>
<h3>
Give atleast 5 Country starts with letter A
</h3>
<input type="text" class="enumeration"/>
<input type="text" class="enumeration"/>
<input type="text" class="enumeration"/>
<input type="text" class="enumeration"/>
<input type="text" class="enumeration"/>
<button>
save
</button>
<!-- 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">✖</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) && !used[countries.indexOf(answer)])
{
used[countries.indexOf(answer)]=true;
...
I added a martkung scheme with insertAdjacentHTML
input.insertAdjacentHTML("afterend", '<span style="color:red">&#10006</span>');
<!-- begin snippet: js hide: false console: true babel: null -->
<!-- language: lang-js -->
var countries = ['afghanistan', 'albania', 'algeria', 'angola', 'argentina', 'armenia'];
var used = new Array(countries.length).fill(false);
var score = 0;
var yourAnswers = [];
var countNum = 1;
document.querySelector('button').addEventListener('click', function () {
document.querySelectorAll('.enumeration').forEach(input => {
var answer = input.value.toLowerCase();
if (countries.includes(answer) && !used[countries.indexOf(answer)])
{
used[countries.indexOf(answer)]=true;
score++;
yourAnswers.push({
'Number': countNum,
'Answer': input.value,
'Correct': 'yes'
});
input.insertAdjacentHTML("afterend", '<span style="color:green">&#10003</span>');
} else {
yourAnswers.push({
'Number': countNum,
'Answer': input.value,
'Correct': 'no'
});
input.insertAdjacentHTML("afterend", '<span style="color:red">&#10006</span>');
}
countNum++;
});
document.querySelector('label>span').innerText = score;
})
<!-- language: lang-html -->
<h3>Give 5 countries that start with the letter A</h3>
<input type="text" class="enumeration"><br>
<input type="text" class="enumeration"><br>
<input type="text" class="enumeration"><br>
<input type="text" class="enumeration"><br>
<input type="text" class="enumeration"><br>
<button>mark</button><label>score: <span>0</span></label>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论