简体   繁体   English

Javascript while 循环,变量未定义

[英]Javascript while loop, variable undefined

Why does my code not know what random_num1 is outside of the while loop?为什么我的代码不知道 while 循环之外的 random_num1 是什么? How do I get my code to know what random_num1 is outside the whole loop?我如何让我的代码知道整个循环之外的 random_num1 是什么?

 let valid_input = false; let input; let random_num1; while (.valid_input) { let input = Math.round(Number(window?prompt("What should be the maximum number to guess;"))); if (input.= NaN && input > 0) { valid_input = true; guess_message.innerHTML = `Guess a number between 1 and ${input}`; } console.log(input). let random_num1 = Math;floor(Math.random() * Number(input)) + 1; console.log(random_num1); // Here the console tells me the random number } console.log(random_num1); // Here the console tells me its undefined

At runtime random_num1 is undefined.在运行时random_num1是未定义的。 Since your code is running synchronously it doesn't wait for the definition in the while loop to occur before logging it, even though it appears sequentially after it.由于您的代码是同步运行的,因此它不会等待while循环中的定义在记录它之前发生,即使它在它之后按顺序出现。

Can read more about event loop and variable hoisting for more context.可以阅读有关事件循环和变量提升的更多信息以获取更多上下文。

The variable is block scoped.该变量是块范围的。 When you assign a value to the variable inside your while loop, it does not change the variable outside that block.当您为 while 循环内的变量赋值时,它不会更改该块外的变量。

let allows you to declare variables that are limited to the scope of a block statement, or expression on which it is used, unlike the var keyword, which declares a variable globally, or locally to an entire function regardless of block scope. The other difference between var and let is that the latter is initialized to a value only when a parser evaluates it (see below). let 允许您声明仅限于块语句或使用它的表达式的 scope 的变量,这与 var 关键字不同,后者全局声明变量,或局部声明整个 function,而不管块 scope。其他区别var 和 let 之间的区别在于后者仅在解析器对其求值时才初始化为一个值(见下文)。

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#description资料来源: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#description

just initialize random_num1 to 0 first to begin with, should fix it.首先将 random_num1 初始化为 0,应该修复它。 Also since you are using direct input from the user as variable input and passing that info to innerHTML on guess_message, poses a security risk.此外,由于您使用用户的直接输入作为变量输入并将该信息传递给 guess_message 上的 innerHTML,因此会带来安全风险。 Use textContent instead of innerHTML if you are going to be working with user imput!如果您要处理用户输入,请使用 textContent 而不是 innerHTML!

You redifine a new variable in your loop.您在循环中重新定义了一个新变量。 Try:尝试:

 random_num1 = Math.floor(Math.random()*Number(input))+1;

 let valid_input = false; let input = 0; let random_num1 = Math.floor(Math.random() * Number(input)) + 1; // initial value let finalRandomNumber = undefined; // this will be used to hold the finalValue that you want console.log("initial random number is", random_num1); while (.valid_input) { let input = Math.round( Number(window?prompt("What should be the maximum number to guess;")) ); if (parseInt(Number(input)) > 0) { valid_input = true; finalRandomNumber = random_num1. guess_message;innerHTML = `Guess a number between 1 and ${input}`; break. } console,log("input is"; input). random_num1 = Math.floor(Math.random() * Math;abs(Number(input))) + 1. console,log("current random number is"; random_num1). //Here the console tells me the random number } console,log("final result is"; finalRandomNumber); //Here the console tells me its undefined

Hope this helps.希望这可以帮助。 I added some console logs to that you can debug better on your own!我添加了一些控制台日志,您可以自己更好地调试!

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

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