javascript 我如何写一个脚本来猜测一个字母从分裂的数组?

yhuiod9q  于 2023-05-16  发布在  Java
关注(0)|答案(1)|浏览(73)

这有点像命运之轮。我已经将一个随机数组条目拆分为单个字母,并且我正在尝试编写一个脚本,让我能够从数组中猜测一个字母。完整代码在下面。

var movietitles = ['Iron Man', ' Jaws', 'Avengers', 'Evil Dead', 'It', 'Transformers', 'Little Mermaid', 'Mulan', 'Scooby Doo', ];

const selection = movietitles[Math.floor(Math.random() * movietitles.length)];

let text = selection;

const game = text.split("");

for (var i = 0; i < game.length; i++) {

  var newEl = document.createElement('p');

  var newNode = document.createTextNode(game[i]);

  newEl.appendChild(newNode);

  document.body.appendChild(newEl);

  var tile = document.getElementsByTagName('p');

}

function guessLetter() {
  alert('Function called!');
  userInput.value = '';
}

var userInput = document.getElementById('entry');

userInput.addEventListener('change', function() {
  guessLetter(userInput.value);
});
p {
  float: left;
  margin: 10px;
  background-color: white;
}

body {
  background-color: lightgray;
  margin-top: 100px;
}

.tile {
  width: 50px;
  height: 50px;
  float: left;
  background-color: black;
  margin: 10px;
}
<div id=display></div>

<span>Please guess a letter or the whole title </span><input type=text id=entry>

我什么都没试过因为我完全被这个难住了

oxiaedzo

oxiaedzo1#

var movietitles = ['Iron Man', 'Jaws', 'Avengers', 'Evil Dead', 'It', 'Transformers', 'Little Mermaid', 'Mulan', 'Scooby Doo'];

const selection = movietitles[Math.floor(Math.random() * movietitles.length)];

let text = selection;

const game = text.split("");
var correctGuesses = new Array(game.length).fill(false);

for (var i = 0; i < game.length; i++) {
  var newEl = document.createElement('p');
  var newNode = document.createTextNode("_");
  newEl.appendChild(newNode);
  document.getElementById("display").appendChild(newEl);
}

function guessWord(guess) {
  var correctGuess = game.join('').toLowerCase() === guess.toLowerCase();
  if (correctGuess) {
    alert("Congratulations! You have correctly guessed the movie title!");
  } else {
    alert("Incorrect guess. Please try again.");
  }
}

var userInput = document.getElementById('entry');

userInput.addEventListener('change', function() {
  guessWord(userInput.value);
  userInput.value = "";
});
p {
  float: left;
  margin: 10px;
  background-color: white;
}

body {
  background-color: lightgray;
  margin-top: 100px;
}

.tile {
  width: 50px;
  height: 50px;
  float: left;
  background-color: black;
  margin: 10px;
}
<div id=display></div>

<span>Please guess a letter or the whole title </span><input type=text id=entry>

相关问题