javascript - 记录到控制台随机选择的数组值
问题描述
我是编码新手,大约一周前开始使用诸如 Mark Myers 的名为“学习 Javascript 的更智能方法”的书和在线教程等资源。我想尝试一些东西,但到目前为止还没有成功。
简而言之,我想从三个数组开始。其中两个将有三个值,第三个最初是一个空数组。我希望将第一个数组的每个值与第二个数组的每个值连接起来,并将结果添加到第三个(最初为空)数组中。由于第一个数组和第二个数组都包含三个值,因此总共将有九种组合,从而在第三个数组中产生九个值。
我想要实现的是为第一个数组的每个值显示可能的三种组合之一。我希望这以随机的方式发生,使用上述书籍已经涵盖的陈述,我已经知道的陈述,而不是徘徊在未知领域。
我的方法是创建三个随机数,每个随机数代表第三个数组中的一个值(更准确地说是索引),它将容纳第一个和第二个数组的组合(连接)。因此,我希望第一个随机数为 0、1 或 2 以“选择”第一个数组的第一个值的可能组合,第二个随机数为 3、4 或 5 以选择可能的组合第一个数组的第二个值,最后是第三个随机数,6、7 或 8 指向第一个数组的第三个值的组合。
目标是在控制台中记录随机选择的组合(连接)——一个用于第一个数组的每个值的组合。换句话说,我希望在日志中看到三个串联。但是,控制台只返回一个连接。
有人可以阐明究竟缺少什么或错误吗?我相信我的代码中的问题在于最后一行,但到目前为止无法弄清楚问题到底是什么。我也不确定为什么 Visual Studio Code(我正在使用“Prettier”扩展名)修改我最后一行的格式
console.log(strNum[num1, num2, num3]);
到
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)]);
解决方案
Prettier 会尝试更正您的代码。你不能有console.log(strNum[num1, num2, num3])
,因为你一次只能从一个数组中选择一个元素。strNum[num1]
应该可以正常工作,因为您传递了一个数字(这是元素的索引)。
当您提供多个变量时,Prettier 会尝试对方括号内的内容进行分组。这就是它添加分组运算符(额外括号)的原因。
您可以一次选择一个元素,如下所示:
function random(min, max) {
return Math.floor((Math.random() * ((max - min) + 1)) + min);
}
let first = random(0, 2);
let second = random(3, 5);
let third = random(6, 8);
// Declaring variables for the operation
let str = ["a ", "b ", "c "];
let num = ["1", "2", "3"];
let strNum = [];
let k = 0;
// Combining the contents of array str and array num and adding the combinations to array strNum
for (let i = 0; i < str.length; i++) {
for (let j = 0; j < num.length; j++) {
strNum[k] = str[i] + num[j];
k++;
}
}
console.log(strNum[first], strNum[second], strNum[third]);
// Or you can log a new array
console.log([strNum[first], strNum[second], strNum[third]]);
我还创建了一个random()
函数,因此您不必重复自己。请不要创建全局变量。在循环中声明i
and 。j
for
推荐阅读
- typescript - TypeScript React Hooks,自定义钩子类型推断问题
- python - 在 SQL [Python/SQLite] 中高效搜索子字符串
- c# - 如何使用 C# 更新 BsonDocument 中的 BsonArray 值?
- c - 为什么向量长度 SIMD 代码比普通 C 慢
- python - 如何将函数应用于 numpy 中的滚动窗口?
- c++ - 有没有办法在不覆盖实际分配的情况下使用 vulkan 内部分配回调?
- javascript - 模式:如何从异步函数返回的结果中访问 object.properties(正确)
- pandas - 熊猫注释框重叠
- c# - Lucene.net - 将文档添加到现有索引会导致查找失败
- scala - 根据条件转换scala列表