gpt4 book ai didi

javascript - 我在将值附加到数组时遇到问题

转载 作者:行者123 更新时间:2023-11-29 23:17:21 25 4
gpt4 key购买 nike

var secretWord = [];
var underScoreWord = [];
// var guesses = [];
var wordLetter = false;

var city = ["Paris", "Wellington", "Hanoi", "Perth", "Marseille", "London", "Ottawa", "Zurich", "Boston", "Tokyo", "Detroit"];

// console.log(city);

// Pick random word from the team array and push the result to an empty array.
// FUNCTION 1 pick random city
function pickRandomCity() {
var randomCity = city[Math.floor(Math.random() * city.length)];
secretWord.push(randomCity);
return randomCity;
}

var cityPicked = pickRandomCity();

// Get length of secretWord and push as underscores to am empty array
for (var i = 0; i < cityPicked.length; i++) {
underScoreWord.push("_");
}

console.log(secretWord);
console.log(underScoreWord);



// Check for letters
//listen for key press and check to see if its a match
document.onkeyup = function letterCheck(event) {
var userGuess = event.key;

for (var j = 0; j < cityPicked.length; j++) {
if (userGuess === cityPicked[j]) {
wordLetter = true;
}
if (wordLetter) {
underScoreWord.push(userGuess);
}
}



console.log(wordLetter);


}

在 onkeyup 函数中,我试图将结果(按下的键)推送到 theunderScoreWord 数组中。当我键入正确的键时,它正在将 wordLetter bool 值转换为 true,但我无法弄清楚如何将它推到单词内部,所以它显示如下 _ _ N _ _ _

我认为我很接近,但我又可能相距数英里。有什么建议吗?

最佳答案

你想通过 underScoreWord 数组中匹配的单词按下按键来填充 _

示例:

  • 如果选择东京作为随机城市,

  • 它将存储 [ "_", "_", "_", "_"]underScoreWord

  • 现在如果使用 T 它将填充 [ "T", "_", "_", "_"]

  • 在使用按下 o 后,它将填充 [ "T", "o", "_", "o"]

Problem :

  • secretWord is array but storing entire word in 1 length
  • fill data in underScoreWord only the same index of the correct spelling match

请检查以下解决方案:

var secretWord = [];
var underScoreWord = [];
var guesses = [];
var wordLetter = false;

var city = ["Paris", "Wellington", "Hanoi", "Perth", "Marseille", "London", "Ottawa", "Zurich", "Boston", "Tokyo", "Detroit"];

// Pick random word from the team array and push the result to an empty array.
// FUNCTION 1 pick random city
function pickRandomCity() {
var randomCity = city[Math.floor(Math.random() * city.length)];
secretWord = randomCity.split('');

return randomCity;
}

var cityPicked = pickRandomCity();

// Get length of secretWord and push as underscores to am empty array
for (var i = 0; i < cityPicked.length; i++) {
underScoreWord.push("_");
}

console.log('secretWord : ' + secretWord);
console.log('underScoreWord : ' + underScoreWord);
console.log('------------------');
console.log('cityPicked : ' + cityPicked);



// Check for letters
//listen for key press and check to see if its a match
document.onkeyup = function letterCheck(event) {
var userGuess = event.key;
for (var j = 0; j < secretWord.length; j++) {
if (userGuess === secretWord[j]) {
wordLetter = true;
underScoreWord[j]= userGuess;
}
}
console.log(underScoreWord);
}

关于javascript - 我在将值附加到数组时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52286951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com