简体   繁体   English

选择随机数后如何保持变量

[英]how to maintain variable after random number is chosen

EDIT: added full code, done on codepen.io https://codepen.io/agnizz/pen/aXgdxR?editors=0011 The css is very basic and will be polished when code is done. 编辑:添加了完整的代码,在codepen.io https://codepen.io/agnizz/pen/aXgdxR?editors=0011上完成。CSS非常基础,在完成代码后将得到完善。

My code takes user input. 我的代码需要用户输入。 The codes purpose is to guess the users number. 该代码的目的是猜测用户数。 Once the number is inputted, you tell the code to guess higher or lower. 输入数字后,您告诉代码猜测更高或更低。 I'm stuck on the point where it needs to keep guessing to eventually read the users number. 我陷入困境,需要不断猜测才能最终读取用户号码。 Once the "random number" is outputted I need to store that number in one session instead of a different number every time. 输出“随机数”后,我需要将该数字存储在一个会话中,而不是每次都存储一个不同的数字。

I've tried creating a variable for the initial random number so it stays consistent. 我尝试为初始随机数创建一个变量,以使其保持一致。 As code guesses original user input. 正如代码猜测的是原始用户输入一样。

function getUserNumber(){
  var userNumber = document.getElementById("userInput").value;
  alert(generateFirstGuess());
  addButtons();
}
//adding higher and lower buttons 
function addButtons(){
  //guess higher btn
  var breakPoint = document.createElement("BR");
  var btn = document.createElement("BUTTON");     
  var guessHigh = document.createTextNode("Guess Higher"); 
  btn.appendChild(guessHigh);                     
  document.forms["form"].appendChild(btn); 
  btn.setAttribute("id", "higherBtn");
  btn.setAttribute("type", "button");
  document.getElementById("higherBtn").addEventListener("click", 
 guessHigher);


  //guess lower button
  var btn = document.createElement("BUTTON");     
  var guessLow = document.createTextNode("Guess Lower"); 
  btn.appendChild(guessLow);                       
  document.forms["form"].appendChild(btn); 
  btn.setAttribute("id", "lowerBtn");
  btn.setAttribute("type", "button");
  document.getElementById("lowerBtn").addEventListener("click", 
guessLower);

  //reset button
  var btn = document.createElement("BUTTON");
  var resetBtn = document.createTextNode("Reset");
  btn.appendChild(resetBtn);
  document.forms["form"].appendChild(btn);
  btn.setAttribute("id", "reset");
  btn.setAttribute("type", "button");
  document.getElementById("reset").addEventListener("click", 
resetBtnFunction);

}



function resetBtnFunction(){
  document.location.reload(true);
}


//guess higher or lower logic
function guessHighOrLow(min, max){
  min = Math.ceil(min);
  max = Math.floor(max);
  var computerGuess = Math.floor(Math.random() * (max - min + 1)) + 
min;
  return computerGuess;
}


//Computer generates number from 1-99
function generateFirstGuess(){
  randomNumber = Math.floor(Math.random() * 100);
  return randomNumber;
}


//computer guesses higher if guess too low

function guessHigher(){
  alert(guessHighOrLow(randomNumber, 100));
}

function guessLower(){
  alert (guessHighOrLow(1, randomNumber));
} 

I need to find a way to make the computerGuess stay the first variable it initializes. 我需要找到一种方法,使computerGuess保持其初始化的第一个变量。

Use a dictionary to store the guessed values 使用字典来存储猜测值

 // dictionary const guessDictionary = {}; //guess higher or lower logic function guessHighOrLow(min, max){ min = Math.ceil(min); // eg: 40 max = Math.floor(max); // eg: 99 // generating a unique key for dictionary eg: "40-99" const key = [min, max].join('-'); // generates and store computerGuess if not in dictionary if(guessDictionary[key] === undefined) { guessDictionary[key] = Math.floor(Math.random() * (max - min + 1)) + min; } // always return from dictionary return guessDictionary[key]; } //Computer generates number from 1-99 function generateFirstGuess(){ randomNumber = Math.floor(Math.random() * 100); return randomNumber; } //computer guesses higher if guess too low function guessHigher(){ alert(guessHighOrLow(randomNumber, 100)); } function guessLower(){ alert (guessHighOrLow(1, randomNumber)); } 

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

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