if语句中的范围问题

时间:2018-07-07 10:49:37

标签: javascript if-statement scope

所以我正在创建一个猜谜游戏。 在一个if语句中,我创建了另外两个if语句,并且在每个if语句中,我给了一个先前定义的变量一个值。 但是,当我想从这些if语句中调用它时,似乎并未重新定义该变量。 为什么?

这是在全局范围内定义的变量:

let computerNumber = Math.floor(Math.random() * 10);
let pastGuesses = [];
let userGuess = Number(prompt("What number do you think it is?"));
let highOrLow = "";
let getting = "";

这里是我重新定义“ getting”值以及稍后在“ userGuess”中调用它的部分,但是该值在if语句中未定义:

 else {
  pastGuesses.push(userGuess);
  let showPastGuesses = pastGuesses.toString();
  if((computerNumber - pastGuesses[i]) < (computerNumber -  
  pastGuesses[i-1])){
  let getting = "getting hotter";
 console.log(`answer is ${getting}`);
} else if((computerNumber - pastGuesses[i]) > (computerNumber - 
pastGuesses[i-1])){
let getting = " getting colder";
console.log(`answer is ${getting}`);
}
  console.log(showPastGuesses);
  userGuess = Number(prompt(`too ${highOrLow}, you are ${getting} though,  
you have ${remainingGuesses} tries left. Your previous guesses were 
${showPastGuesses}`));
}

这是完整的代码

let computerNumber = Math.floor(Math.random() * 10);
let pastGuesses = [];
let userGuess = Number(prompt("What number do you think it is?"));
let highOrLow = "";
let getting = "";

function showChoices() {
  console.log(`the computer chose ${computerNumber}`);
  console.log(`your number is ${userGuess}`);
};

for (var i = 0; i < 10; i++) {
  if (Number.isNaN(userGuess) || userGuess < 0) {
    userGuess = prompt("WHAT YOU DOIN, we said a number ...");
  } else if (userGuess < computerNumber) {
    let highOrLow = "low";
    let remainingGuesses = 10 - i;
    showChoices();
    if (pastGuesses[0] === undefined) {
      pastGuesses.push(userGuess);
      let showPastGuesses = pastGuesses.toString();
      userGuess = Number(prompt(`too ${highOrLow}, you have                                          
  ${remainingGuesses} tries left. Your previous guesses were   
  ${showPastGuesses}`));
    } else {
      pastGuesses.push(userGuess);
      let showPastGuesses = pastGuesses.toString();
      if ((computerNumber - pastGuesses[i]) < (computerNumber -
          pastGuesses[i - 1])) {
        let getting = "getting hotter";
        console.log(`answer is ${getting}`);
      } else if ((computerNumber - pastGuesses[i]) > (computerNumber -
          pastGuesses[i - 1])) {
        let getting = " getting colder";
        console.log(`answer is ${getting}`);
      }
      console.log(showPastGuesses);
      userGuess = Number(prompt(`too ${highOrLow}, you are ${getting} though,   
  you have ${remainingGuesses} tries left. Your previous guesses were  
  ${showPastGuesses}`));
    }
  } else if (userGuess > computerNumber) {
    let highOrLow = "high";
    let remainingGuesses = 10 - i;
    showChoices();
    if (pastGuesses[0] === undefined) {
      pastGuesses.push(userGuess);
      let showPastGuesses = pastGuesses.toString();
      userGuess = Number(prompt(`too ${highOrLow}, you have 
  ${remainingGuesses} tries left. Your previous guesses were 
  ${showPastGuesses}`));
    } else {
      pastGuesses.push(userGuess);
      let showPastGuesses = pastGuesses.toString();
      if ((computerNumber + pastGuesses[i]) < (computerNumber +
          pastGuesses[i - 1])) {
        let getting = "getting hotter";
        console.log(`answer is ${getting}`);
      } else if ((computerNumber + pastGuesses[i]) > (computerNumber +
          pastGuesses[i - 1])) {
        let getting = " getting colder";
        console.log(`answer is ${getting}`);
      }
      console.log(showPastGuesses);
      userGuess = Number(prompt(`too ${highOrLow},you are ${getting} though,   
  you have ${remainingGuesses} tries left. Your previous guesses were 
  ${showPastGuesses}`));
    }
  } else {
    showChoices();
    alert("Well done, that's the right number");
    break;
  }
}

0 个答案:

没有答案