gpt4 book ai didi

JavaScript!创建一个猜单词游戏

转载 作者:行者123 更新时间:2023-12-03 02:52:31 25 4
gpt4 key购买 nike

代码如下:

  let ccdisplay = document.querySelector('.crrDisplay');
let incdisplay = document.querySelector('.incDisplay');
let guess = document.querySelector('#character');
let textForm = document.querySelector('.textForm');

var commonWords = [
"the", "of", "and", "a", "to", "in", "is", "you", "that", "it",
"he", "was", "for", "on", "are", "as", "with", "his", "they","I", "at",
"be","this", "have", "from", "or", "one", "had", "by", "word", "but","not",
"what", "all", "were", "we", "when", "your", "can", "said", "there",
"use", "an", "each", "which", "she", "do", "how", "their", "if",
"will","up", "other", "about", "out", "many", "then", "them",
"these", "so","some", "her", "would", "make", "like", "him", "into", "time", "has",
"look", "two", "more", "write", "go", "see", "number", "no", "way",
"could", "people", "my", "than", "first", "water", "been", "call",
"who", "oil", "its", "now", "find", "long", "down", "day", "did",
"get", "come", "made", "may", "part"];


// Grabbing Random Word
var chooseRandomWord = function(array) {
return array[Math.floor(Math.random() * array.length)];
}

var chosenWord = chooseRandomWord(commonWords);
console.log(chosenWord)

// Function that submits the values
textForm.addEventListener('submit', function(event) {

var counter = 10;
var triedCharacters = [];
var correctCharacters = [];

event.preventDefault();
guess = character.value

for (i = 0; i < chosenWord.length; i++) {
chosenWord[i]
for (z = 0; z < guess.length; z++) {
if (guess[z] === chosenWord[i]) {
correctCharacters.push(guess[z])
console.log("correct " + correctCharacters)
}
else {
triedCharacters.push(guess[z])
console.log("incorrect " + triedCharacters)
}
};
}
})

嘿,我正在尝试创建一个游戏,猜测随机单词并将正确的单词放入一个数组中,将错误的字符放入另一个数组中正确的数组有效,但不正确的其他无效并插入每个字符.

最佳答案

应该只有一个循环。您还应该循环遍历较短的单词,但带有单词长度的提示会很好...

let display = document.querySelector('.display');
let guessQuerySelector = document.querySelector('#character');
let textForm = document.querySelector('.textForm');

var commonWords = [
"the", "of", "and", "a", "to", "in", "is", "you", "that", "it", "he",
"was", "for", "on", "are", "as", "with", "his", "they", "I", "at", "be",
"this", "have", "from", "or", "one", "had", "by", "word", "but", "not",
"what", "all", "were", "we", "when", "your", "can", "said", "there",
"use", "an", "each", "which", "she", "do", "how", "their", "if", "will",
"up", "other", "about", "out", "many", "then", "them", "these", "so",
"some", "her", "would", "make", "like", "him", "into", "time", "has",
"look", "two", "more", "write", "go", "see", "number", "no", "way",
"could", "people", "my", "than", "first", "water", "been", "call",
"who", "oil", "its", "now", "find", "long", "down", "day", "did", "get",
"come", "made", "may", "part"
];

// Grabbing Random Word
var getRandomWord = function(array) {
return array[Math.floor(Math.random() * array.length)];
}

var randomWord = getRandomWord(commonWords);
console.log('randomWord', randomWord);

// Function that submits the values
textForm.addEventListener('submit', function(event) {
event.preventDefault();

var counter = 10;
var triedCharacters = [];
var correctCharacters = [];

var guessWord = guessQuerySelector.value;
var shorterWordlength = randomWord.length > guessWord.length ? guessWord.length : randomWord.length;

console.log('guessWord', guessWord);

for (i = 0; i < shorterWordlength; i++) {
if (guessWord[i] === randomWord[i]) {
correctCharacters.push(guessWord[i])
console.log("correct " + correctCharacters)
} else {
triedCharacters.push(guessWord[i])
console.log("incorrect " + triedCharacters)
}
}
randomWord = getRandomWord(commonWords);
console.log('randomWord', randomWord);
})

关于JavaScript!创建一个猜单词游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47787313/

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