简体   繁体   English

JS for Kids Hangman游戏

[英]JS for Kids Hangman game

I don't know what the [i] in answerArray[i] does/means. 我不知道是什么[i]answerArray[i]确实/手段。 If someone could explain what it does it would mean a lot. 如果有人能够解释它的作用,那将意味着很多。 This code came from the book "JavaScript for kids" and I'm trying to get started with just any kind of coding 这段代码来自《 JavaScript for Kids》一书,我正尝试从任何一种编码开始

var words = [
"money",
"pillow",
"maracas"
];

var word = words[Math.floor(Math.random() * words.length)];

var answerArray = [];

Here 这里

for (var i = 0; i < word.length; i++) {
*answerArray[i] = "_";* 
}

var remainingLetters = word.length;

while (remainingLetters > 0) {
alert(answerArray.join(" "));
var guess = prompt("Guess a letter, or click cancel to stop playing.");
if (guess === null) {
break;
} else if (guess.length !== 1) {
alert("Please enter a single letter.");
} else {

And here 和这里

*for (var j = 0; j < word.length; j++) {
if (word[j] === guess) {
 answerArray[j] = guess;*
 remainingLetters--;
}
}
}
}

alert(answerArray.join(" "));
alert("Good job! The answer was " + word);

Here's an example 这是一个例子

https://www.w3schools.com/js/tryit.asp?filename=tryjs_loop_for_ex https://www.w3schools.com/js/tryit.asp?filename=tryjs_loop_for_ex

The letters i or j or litterally anything put in that slot when you write for (i = 0; i < 5; i++) represents the current itteration . 当您for (i = 0; i < 5; i++)编写时,字母ij或什至放置在该插槽中的所有字母代表当前的itteration This is thing called a for loop and you should definitely get to know it if you are just getting started with javascript. 这就是所谓的for loop ,如果您刚开始使用javascript,则一定应该了解它。

The letter, in short, represents the current itteration of the loop. 简而言之,字母代表环路的当前信号。

for (i = 0; i < 5; i++)  { }

What this loop is saying is first i = 0 . 这个循环的意思是首先i = 0 the variable "i" is 0. Then it says i < 5 . 变量“ i”为0。然后说i < 5 This part is the "test". 这部分是“测试”。 If the test passes, the loop with run again. 如果测试通过,则循环再次运行。 Then it says i++ . 然后说i++ This is something that happens after the loop has been run. 这是在循环运行之后发生的事情。 In this example it increases i with 1. 在此示例中, i增加1。

What happens inside { } will occure 5 times, and the letter i will increase in value, representing the "current loop". { }内部发生的情况将发生5次,并且字母i值将增加,表示“当前循环”。

In the demo i linked, you see that the sentence "The number is x" appears in increasing order, starting with 0. 在链接的演示中,您看到句子“数字为x”以从0开始的递增顺序出现。

The loop essentially means "While i is less than 5, do this" and the i increases in value each loop and the test inside () is rerun. 该循环本质上意味着“当i小于5时,执行此操作”,并且每个循环i的值都会增加,并且()的测试将重新运行。

In arrays, each slot is represented by a number. 在阵列中,每个插槽均由一个数字表示。 MyArray[1] refers to the second item in the array. MyArray[1]引用数组中的第二项。 If you have an array like this var myArray = ["fi", "fa", "fo"] then you can write console.log[2] to print the third item. 如果您有一个类似var myArray = ["fi", "fa", "fo"]的数组var myArray = ["fi", "fa", "fo"]则可以编写console.log[2]来打印第三项。

Lets combine this knowledge like this! 让我们结合这样的知识!

var myArray = ["fi", "fa", "fo"];

for (i = 0; i < 3; i++) {
   alert(myArray[i]);
}

The for-loop is run 3 times, and each time it is run, i has a bigger value. for循环运行3次,每次运行, i都有一个更大的值。 This lets us refer to every item in the array. 这使我们可以引用数组中的每个项目。 A better loop to write would be for(var i = 0; i < myArray.length; i++ . This makes us compare i to the size of your array, so the array can be any size it wants to be 一个更好的循环应该是for(var i = 0; i < myArray.length; i++ 。这使我们将i与数组的大小进行比较,因此数组可以是它想要的任何大小

1 > the answer array initially empty array=[]; 1>答案数组最初为空array = [];

2 > then words[Math.floor(Math.random() * words.length)] this will return a random string from words array . 2>然后是words[Math.floor(Math.random() * words.length)]这将从words数组返回一个随机字符串。

3 > then the for loop is just counting the number of characters present in the selected word and that same time number of _ is inserted in answers Array 3> for循环仅计算所选单词中存在的字符数,并且在答案中插入相同的_

4 > then this will just joining all the underscores and make a single string (with in the spaces). 4>然后,这将仅连接所有下划线并组成单个string (在空格中带有)。

eg -> word " pillow " is selected 例如->选择单词“ 枕头

then answers Array= ['_','_','_','_','_','_'] 然后回答Array = ['_','_','_','_','_','_']

then answerArray= "_ _ _ _ _ _" ( Join with spaces). 然后answerArray = "_ _ _ _ _ _" (以空格连接)。

Thanks. 谢谢。

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

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