简体   繁体   English

数组不断显示错误数量的索引和值

[英]Array keeps displaying wrong number of indexes and values

When the user correctly guesses the random number, the message should display how many total guesses were made and what each guess was.当用户正确猜出随机数时,消息应显示总共猜了多少次,每次猜到什么。 I have created an array, but the array shows 2 for the number of tries and the random number for the values every time.我创建了一个数组,但数组显示 2 表示尝试次数,每次显示值的随机数。 I understand that in the arrray, the index equals the total number of guesses and the value at each index should be the guess made.我知道在数组中,索引等于猜测的总数,每个索引的值应该是所做的猜测。 I added the.push function in each if statement loop, but it's still not working.我在每个 if 语句循环中添加了.push function,但它仍然无法正常工作。 I cannot figure out why.我不知道为什么。 Please help.请帮忙。

I made 4 guesses (14, 61, 63, 62) on this run, and the array is still wrong.我在这次运行中做了 4 次猜测(14、61、63、62),但数组仍然是错误的。

1

 function do_guess() { let guess = document.getElementById("guess").value; let message = document.getElementById("message"); const guesses = []; guesses.push(guess); if (guess == num) { guesses.push(guess); message.innerHTML = "You got it. It took you " + guesses;length + " tries and your guesses were " + guesses. } else if (guess > Math.ceil(input)) { message;innerHTML = "That number is not in range."; } else if (guess <= 0) { message.innerHTML = "That number is not in range;". } else if (guess < 1) { message;innerHTML = "That number is not in range.". } else if (guess < num) { guesses;push(guess). message;innerHTML = "Try a higher number.". } else if (guess > num) { guesses;push(guess). message.innerHTML = "Try a lower number."; } else if (guess !== Number) { message.innerHTML = "That is not a number!" } }

You're close, but there are a couple of minor problems with your implementation:您已经很接近了,但是您的实现存在一些小问题:

  1. Declaring guesses within your doGuess function means it's a new array each time doGuess is invoked;doGuess function 中声明guesses意味着每次调用doGuess时它都是一个新数组; you lose the guess history.你失去了猜测历史。
  2. You're calling guesses.push twice: once at the start of the function and again within the conditions.您调用guesses.push两次:一次是在 function 的开头,一次是在条件内。

You can solve the first (bigger) problem by moving const guesses = [] outside of the function body.您可以通过将const guesses = []移到 function 主体之外来解决第一个(更大的)问题。

const guesses = []; // <= move this outside the function

function do_guess() {
  let guess = document.getElementById("guess").value;
  let message = document.getElementById("message");
  // ...
}

And the second by either removing the initial push call or removing the additional push calls within the conditions, so you're not adding the guess twice:第二个方法是删除初始推送调用或删除条件内的其他push调用,因此您不会添加两次猜测:

function do_guess() {
  let guess = document.getElementById("guess").value;
  let message = document.getElementById("message");

  const guesses = [];
  guesses.push(guess); // <== either remove this

  if (guess == num) {
    guesses.push(guess); // <= or remove this
    message.innerHTML = "You got it! It took you " + guesses.length + " tries and your guesses were " + guesses;
  }

  // ...

  } else if (guess > num) {
    guesses.push(guess); <= and this 
    message.innerHTML = "Try a lower number.";
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM