【发布时间】:2017-03-09 12:43:37
【问题描述】:
我正在尝试制作 Guess Who 猜谜游戏的简单版本。我有一些全局变量来存储东西,例如...
var allTheCharacters = [];
var allTheCharactersComp = [];
var theHuman = []; //Random Character stored here
var theComputer = []; //Random Character stored here
var singleChar;
[{
"id": 1,
"male": true,
"female": false,
"name": "Jimmy",
"bald": false,
"black hair": true,
"white hair": false,
"blonde hair": false,
"red hair": false,
"purple hair": false,
"blue hair": false,
"beard": false,
"moustache": false,
"glasses": false,
"earrings": false,
"hat": true,
"brown skin": true,
"pale skin": false,
"image": "img/face1.jpg"
}, {
"id": 2,
"male": false,
"female": true,
"name": "Vanessa",
"bald": false,
"black hair": false,
"white hair": false,
"blonde hair": true,
"red hair": false,
"purple hair": false,
"blue hair": false,
"beard": false,
"moustache": false,
"glasses": false,
"earrings": false,
"hat": false,
"brown skin": false,
"pale skin": true,
"image": "img/face2.jpg"
}, {
"id": 3,
"male": true,
"female": false,
"name": "Benjamin",
"bald": true,
"black hair": false,
"white hair": true,
"blonde hair": false,
"red hair": false,
"purple hair": false,
"blue hair": false,
"beard": false,
"moustache": true,
"glasses": false,
"earrings": false,
"hat": false,
"brown skin": false,
"pale skin": true,
"image": "img/face3.jpg"
}]
var newGame = function() {
$('.start-button').one('click', function(event) {
turn == 0;
humanTurn();
//checkForWinner();
turn == 1;
computerTurn();
//checkForWinner();
});
};
$(document).ready(function() {
newGame();
});
//Get <SELECT> values
function grabInputValue() {
// click event on select submit
$(".pickAFeatureBtn").on('click', function(e) {
e.preventDefault();
humanTurn();
console.log(turn);
});
}
grabInputValue();
function humanTurn() {
allTheCharacters;
//console.log(allTheCharactersComp);
for(i=allTheCharactersComp.length; i >= 0; i-- ){
//console.log(allTheCharactersComp[i]);
var singleChar = allTheCharactersComp[i];
//
$.each(singleChar, function(index, val) {
// console.log(index, val);
var hasFeature = ($('.featureList').val());
if(index == hasFeature && val == false){
console.log(index + " : " + val);
//Need to remove object from allThe Characters
allTheCharactersComp.splice(index, 1);
}
});
}
console.log(allTheCharactersComp);
turn = 1;
// console.log(turn);
};
function computerTurn() {
// To come...
// Will this just be similar to the humanTurn() code?
};
function checkForWinner() {
// To come...
// check for allTheCharacters.length = 1;
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<button type="submit" class="pickAFeatureBtn">Check for Attribute</button>
<select class="featureList">
<option class="charFeature" value="male">male</option>
<option class="charFeature" value="female">female</option>
<option class="charFeature" value="black hair">black hair</option>
<option class="charFeature" value="white hair">white hair</option>
<option class="charFeature" value="blonde hair">blonde hair</option>
<option class="charFeature" value="red hair">red hair</option>
<option class="charFeature" value="blue hair">blue hair</option>
<option class="charFeature" value="purple hair">purple hair</option>
<option class="charFeature" value="bald">bald</option>
<option class="charFeature" value="beard">beard</option>
<option class="charFeature" value="moustache">moustache</option>
<option class="charFeature" value="glasses">glasses</option>
<option class="charFeature" value="earrings">earrings</option>
<option class="charFeature" value="hat">hat</option>
<option class="charFeature" value="green eyes">green eyes</option>
<option class="charFeature" value="black eyes">black eyes</option>
<option class="charFeature" value="brown skin">dark skin</option>
<option class="charFeature" value="pale skin">light skin</option>
</select>
所以 - 在 humanTurn() 函数中,我要做的是检查下拉列表的值是否与 theComputer[] 中保存的随机字符相同,如果是,则从 allTheCharactersComp[] 中删除该字符 但是,当我 console.log 的效果 - allTheCharacters.splice(index, 1) - 它只返回数组中的一个对象,而不是删除所有与 if 语句中的条件不匹配的字符,留下那些这样做。
感谢您阅读本文,我知道这里有很多东西,但我不知道如何让它发挥作用。任何帮助将不胜感激。
如果您需要查看任何其他代码,请大声说出来。 再次感谢。
【问题讨论】:
-
splice返回删除的数组元素,在拼接后对数组执行console.log,它应该只有其他元素您还应该尝试使用filter而不是迭代数组 -
谢谢,我试过了。但是,每次循环迭代的结果 console.logs 仍然会一直下降到一个结果。
标签: javascript jquery arrays json logic