Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将随机选择的数组值记录到控制台_Javascript_Arrays_Logging_Random - Fatal编程技术网

Javascript 将随机选择的数组值记录到控制台

Javascript 将随机选择的数组值记录到控制台,javascript,arrays,logging,random,Javascript,Arrays,Logging,Random,我对编码是新手,大约一周前开始使用马克·迈尔斯(Mark Myers)的书《学习Javascript的更聪明的方法》和在线教程等资源。我想尝试一些东西,但到目前为止没有成功。 简而言之,我想从三个数组开始。其中两个将分别具有三个值,第三个最初为空数组。我希望将第一个数组的每个值与第二个数组的每个值连接起来,并将结果添加到第三个最初为空的数组中。由于第一个和第二个数组各包含三个值,因此总共有九个组合,从而在第三个数组中产生九个值 我想要实现的是为第一个数组的每个值显示可能的三种组合之一。我希望这一

我对编码是新手,大约一周前开始使用马克·迈尔斯(Mark Myers)的书《学习Javascript的更聪明的方法》和在线教程等资源。我想尝试一些东西,但到目前为止没有成功。

简而言之,我想从三个数组开始。其中两个将分别具有三个值,第三个最初为空数组。我希望将第一个数组的每个值与第二个数组的每个值连接起来,并将结果添加到第三个最初为空的数组中。由于第一个和第二个数组各包含三个值,因此总共有九个组合,从而在第三个数组中产生九个值

我想要实现的是为第一个数组的每个值显示可能的三种组合之一。我希望这一切以一种随机的方式发生,使用前面提到的书中已经涵盖的陈述,那些我已经意识到的,而不是徘徊在未知的领域

我的方法是创建三个随机数,每个随机数表示第三个数组中的一个值(更准确地说是索引),该数组将容纳第一个和第二个数组的组合(串联)。因此,我希望第一个随机数为0、1或2,以“选择”第一个数组的第一个值的可能组合,第二个随机数为3、4或5,以选择第一个数组的第二个值的可能组合,最后,第三个随机数为6,7或8指向第一个数组的第三个值的组合

目标是在控制台中记录随机选择的组合(串联)第一个数组的每个值一个。换句话说,我希望在日志中看到三个连接。但是,控制台只返回一个连接

有人能解释一下到底是什么遗漏或错误吗?我相信我的代码中的问题在于最后一行,但到目前为止,我还不能确定到底是什么问题。我也不确定为什么VisualStudio代码(我使用的是“Prettier”扩展)会修改我的

console.log(strNum[num1, num2, num3]);

保存文件时,但这可能与我的问题无关

我的代码如下:

// Declaring variables for the operation
var str = ["a ", "b ", "c "]; // Array str to hold strings a, b and c
var num = ["1", "2", "3"]; // Array num to hold numbers 1, 2 and 3
var strNum = []; // Array strNum to hold all possible combinations of array str and array num
var k = 0; //Counter for array strNum

// Combining the contents of array str and array num and adding the combinations to array strNum
for (i = 0; i < str.length; i++) {
  for (j = 0; j < num.length; j++) {
    strNum[k] = str[i] + num[j];
    k++;
  }
}

// The first random number between with the possible values 0, 1 or 2, to determine the first pair
var bigDecimal1 = Math.random();
var improvedDecimal1 = bigDecimal1 * 3;
var RandomNum1 = Math.floor(improvedDecimal1);

// The second random number between with the possible values 3, 4 or 5, to determine the second pair
var bigDecimal2 = Math.random();
var improvedDecimal2 = bigDecimal2 * 3 + 3;
var randomNum2 = Math.floor(improvedDecimal2);

// The third - and last - random number between with the possible values 6, 7 or 8, to determine the third pair
var bigDecimal3 = Math.random();
var improvedDecimal3 = bigDecimal3 * 3 + 6;
var randomNum3 = Math.floor(improvedDecimal3);

console.log(strNum[(num1, num2, num3)]);
//为操作声明变量
var str=[“a”、“b”、“c”];//数组str用于保存字符串a、b和c
var num=[“1”、“2”、“3”];//数组num用于保存数字1、2和3
var strNum=[];//数组strNum用于保存数组str和数组num的所有可能组合
var k=0//数组strNum的计数器
//组合数组str和数组num的内容,并将组合添加到数组strNum
对于(i=0;i
Prettier尝试更正您的代码。您不能有
console.log(strNum[num1,num2,num3])
,因为一次只能从数组中选择一个元素<当您传递一个数字(这是元素的索引)时,code>strNum[num1]应该可以正常工作。当您提供多个变量时,Prettier会尝试将方括号内的内容分组。这就是为什么他们使用(额外的括号)

您可以一次拾取一个元素,如下所示:

随机函数(最小值、最大值){
返回Math.floor((Math.random()*((max-min)+1))+min);
}
设first=random(0,2);
设秒=随机(3,5);
设第三个=随机(6,8);
//声明操作的变量
设str=[“a”,“b”,“c”];
设num=[“1”、“2”、“3”];
设strNum=[];
设k=0;
//组合数组str和数组num的内容,并将组合添加到数组strNum
for(设i=0;ilog([strNum[first],strNum[second],strNum[third]])不是100%清楚,但这是你想要的吗<代码>console.log([strNum[RandomNum1],strNum[randomNum2],strNum[randomNum3]])哦,是的!这正是我想要的!我想我可以简单地一个接一个地“列出”这三个随机数。我还注意到我在代码的最后一行错误地命名了随机数(它们的名称与声明的变量名称不同,事实上,我在前面重命名变量时忘记了重命名它们)。你的决心和我想象的一模一样。非常感谢。Köszi szépen,Zsolti!非常感谢。我还不熟悉函数,它们将在我的书的下一章中介绍。我还注意到我在代码的最后一行错误地命名了随机数。最初,我的随机数变量被称为num1、num2和num3,但为了更清楚起见,我将它们重命名为randomNum1、randomNum2和randomNum3,但在最后一行我忘记了重命名它们。新手犯的错误。谢谢你的帮助,噢,真是个惊喜,又是一个惊喜。那太好了:)继续读那本书,在读完接下来的章节后不要放弃。此外,如果你认为这是有益的,你应该考虑投票和/或接受我的答案。干杯;)谢谢你,佐尔蒂!I c
// Declaring variables for the operation
var str = ["a ", "b ", "c "]; // Array str to hold strings a, b and c
var num = ["1", "2", "3"]; // Array num to hold numbers 1, 2 and 3
var strNum = []; // Array strNum to hold all possible combinations of array str and array num
var k = 0; //Counter for array strNum

// Combining the contents of array str and array num and adding the combinations to array strNum
for (i = 0; i < str.length; i++) {
  for (j = 0; j < num.length; j++) {
    strNum[k] = str[i] + num[j];
    k++;
  }
}

// The first random number between with the possible values 0, 1 or 2, to determine the first pair
var bigDecimal1 = Math.random();
var improvedDecimal1 = bigDecimal1 * 3;
var RandomNum1 = Math.floor(improvedDecimal1);

// The second random number between with the possible values 3, 4 or 5, to determine the second pair
var bigDecimal2 = Math.random();
var improvedDecimal2 = bigDecimal2 * 3 + 3;
var randomNum2 = Math.floor(improvedDecimal2);

// The third - and last - random number between with the possible values 6, 7 or 8, to determine the third pair
var bigDecimal3 = Math.random();
var improvedDecimal3 = bigDecimal3 * 3 + 6;
var randomNum3 = Math.floor(improvedDecimal3);

console.log(strNum[(num1, num2, num3)]);