다음 코드에 문제가 있습니다. 의도하지 않은 일련의 개체에서 중복 된 출력
Object {x: 5, y: 0}
Object {x: 7, y: 0}
Object {x: 7, y: 5}
Object {x: 5, y: 2}
Object {x: 2, y: 1}
Object {x: 7, y: 5}
왜이 코드가 제거되지 않은 : 내가 가끔
outputs with duplicates (image) 같은 얻을 것이다,
{x: val, y: val2}
그러나
로 포맷 객체의 배열에서 (세 번째이자 마지막 항목) 중복을 제거하도록 설계 그것이 예정되어있는대로 중복됩니까?var mines = [{}];
console.log("\n\n\n\n\n");
for (var i = 0; i < 10; i++){ //for each mine that needs to be made
var mine = {}; //initalize temporary object
var dupeerr = false; //set duplication error flag to false
//(will be flipped if loop needs to be re-executed)
do{
//generate coordinates for new mine
//(random int between 0 and max size), then rounded to
mine.x = Math.round(Math.random()*(8));
mine.y = Math.round(Math.random()*(9));
for (var j = mines.length - 1; j >= 0; j--) { //for each mine in array
if ((mines[j].x == mine.x) && (mines[j].y == mine.y)) { //check for doubles
dupeerr = true; //flag for re-execution
} else if (mines[j] != mine) {
dupeerr = false;
}
};
//console.log(mines);
} while (dupeerr || bounderr);
mines[i] = mine;
}
var ans = [];
for (var i = 0; i < mines.length; i++) {
ans[i] = mines[i];
var minebtn = document.getElementById(JSON.stringify(ans[i]));
console.log(ans[i]);
};
복제본에'{x : 7, y : 5} '가 있습니까? – elclanrs
"이 코드가 중복을 제거하지 못하는 이유는 무엇입니까"또는 "어떻게하면 좋을까요?" 왜냐하면 후자라면 멋지게 창의적인 대답이 있기 때문입니다. –