So I'm creating a guessing game. Inside one of the if statement I created two other if statements and inside each I gave a value to a previously defined variable. However when I want to call it out of those if statements, the variable doesn't seem to have been redefined. Why ?
here are the variables defined in global scope :
let computerNumber = Math.floor(Math.random() * 10);
let pastGuesses = [];
let userGuess = Number(prompt("What number do you think it is?"));
let highOrLow = "";
let getting = "";
Here is the part where I redefine the value of "getting" and where I call it later in "userGuess", but the value is undefined out of the if statement :
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}`));
}
And here is the full code
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;
}
}
Aucun commentaire:
Enregistrer un commentaire