[英]Could anyone point to me the error here i can't find it
So this is my last task for FreeCodeCamp, i've asked on the chat room but nobody could point me the error. 所以这是我对FreeCodeCamp的最后一项任务,我在聊天室问过,但没人能指出我的错误。 The problem is that the pattern the computer plays starts with the last element, then goes from start to finish, repeating the last element, which is confusing for the player.
问题在于计算机播放的模式从最后一个元素开始,然后从头到尾重复执行最后一个元素,这对播放器造成了困扰。
$(document).ready( function() {
var gamestarted = false;
var arrTones = []; // track all game tones from the begining
var arrPlayer = []; // track all user tones played
var arrComp = []; // track all comp tones played
var steps = null;
var playerTurns = null;
var strict = null;
function playAudio(id) {
var audio = 'audio' + id ;
audio = document.getElementById(audio);
audio.play();
}
$('.audio').click(function () {
var id = Number( $(this).attr('id') );
playAudio(id);
if(gamestarted) {
arrPlayer.push(id);
if ( arrPlayer[playerTurns] != arrComp[playerTurns] ) {
if(strict) {
alert('Start a new game');
setTimeout( start, 1000 );
} else {
alert('Here is the pattern again');
setTimeout( nextstep, 1000 );
}
} else if ( arrPlayer.length < arrComp.length ) {
playerTurns++;
} else if ( arrPlayer.length == arrComp.length && steps < 20) {
steps++;
setTimeout( nextstep , 1000);
}
}
});
function start() {
// reset of all arrays and the steps
arrTones = [];
arrPlayer = [];
arrComp = [];
steps = 0;
gamestarted = true;
strict = $('#strict').prop('checked');
// generate all moves for this game
for (var i=1; i<=20; i++) {
val = Math.floor(Math.random() * (4 - 1 + 1) + 1);
arrTones.push(val);
}
// get first comp move
nextstep();
}
$('#start').click(function () {
start();
});
function nextstep() {
var tones = 0;
arrComp = [];
for (var x=0; x<=steps; x++) {
arrComp.push(arrTones[x]);
}
var moves = setInterval(function() {
compTurn(arrComp[tones]);
tones++;
if ( tones >= arrComp.legth ) {
clearInterval(moves);
}
}, 1000);
playerTurn();
}
function compTurn(id) {
var button = '#'+id;
$(button).addClass('btn-outline-secondary');
playAudio(id);
setTimeout( function () {
$(button).removeClass('btn-outline-secondary');
}, 300);
}
function playerTurn() {
arrPlayer = [];
playerTurns=0;
}
});
Here is the codepen code for this Simon Game 这是此西蒙游戏的codepen代码
You have misspelled property name 'length' 您拼写了属性名称“ length”
if ( tones >= arrComp.legth ) {
clearInterval(moves);
}
change it to arrComp.length it will work. 将其更改为arrComp.length它将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.