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

时间:2019-03-24 04:26:14

标签: javascript

编辑:添加了完整的代码,在codepen.io上完成 https://codepen.io/agnizz/pen/aXgdxR?editors=0011 css非常基础,在完成代码后会被完善。

我的代码需要用户输入。该代码的目的是猜测用户数。输入数字后,您告诉代码猜测更高或更低。我陷入困境,需要不断猜测才能最终读取用户号码。输出“随机数”后,我需要将该数字存储在一个会话中,而不是每次都存储一个不同的数字。

我尝试为初始随机数创建一个变量,以使其保持一致。代码会猜测原始用户输入。

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));
} 

我需要找到一种使computerGuess保持其初始化的第一个变量的方法。

1 个答案:

答案 0 :(得分:0)

使用字典存储猜测值

// 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));
}