无法从循环中获取值并将其推送到循环外的数组
2019-12-05
280
我创建了一种方法,获取一个新的随机值列表,找到该列表的平均值,然后从以相同方式使用不同数字列表创建的另一个数字中减去该平均值。我希望这个循环循环一定次数,但它只会循环列表一次。
这里的问题是
list_of_outputted_numbers
只会推送
mean_diff
一次,而不是指定的次数。
// Below are the lists of the numbers (the original dataset)
var array_of_nums1 = [55.5, 51, 54.5, 53.5, 51, 59.5, 54, 42, 53.5, 40];
var array_of_nums2 = [60, 64.5, 66.5, 67, 62, 55, 62.5, 64, 65, 55];
// Here I collect how many times the user wants to loop over this mess.
var amount_of_loops = prompt("How many times would you like to loop over the list?", "Numbers only, please and thank you");
var sanitized_input = Number(amount_of_loops);
// This is SUPPOSED TO BE the array in which array that holds the final values are held in.
var list_of_outputted_numbers = [];
for (i = 0; i < sanitized_input; i++) {
// Below I am creating a new array that will contain the a randomized selection of 10 numbers from the original list.
var random_array_of_nums1 = [];
for (i = 0; i < array_of_nums1.length; i++) {
var rand_NUM_array1 = array_of_nums1[Math.floor(Math.random() * array_of_nums1.length)];
random_array_of_nums1.push(rand_NUM_array1)
}
// Here I'm doing the same thing I did ealier, but with the second list
var random_array_of_nums2 = [];
for (i = 0; i < array_of_nums2.length; i++) {
var rand_NUM_array2 = array_of_nums2[Math.floor(Math.random() * array_of_nums2.length)];
random_array_of_nums2.push(rand_NUM_array2)
}
// Here I'm finding the total of the randomized first list, than the total of the elements of the randomized second list.
var rand_array_total1 = 0;
for (var i = 0; i < random_array_of_nums1.length; i++) {
rand_array_total1 += random_array_of_nums1[i];
}
var rand_array_total2 = 0;
for (var i = 0; i < random_array_of_nums2.length; i++) {
rand_array_total2 += random_array_of_nums2[i];
}
// Here I am finding the mean of all the numbers of the randomized elements list.
var mean1 = rand_array_total1 / random_array_of_nums1.length;
var mean2 = rand_array_total2 / random_array_of_nums2.length;
// Difference between the means
var mean_diff = mean2 - mean1;
list_of_outputted_numbers.push(mean_diff);
}
console.log(list_of_outputted_numbers);
2个回答
您使用相同的 i 变量来控制循环,因此 i 仅在第一次迭代中超过了 sanitized_input。
// Below are the lists of the numbers (the original dataset)
var array_of_nums1 = [55.5, 51, 54.5, 53.5, 51, 59.5, 54, 42, 53.5, 40];
var array_of_nums2 = [60, 64.5, 66.5, 67, 62, 55, 62.5, 64, 65, 55];
// Here I collect how many times the user wants to loop over this mess.
var amount_of_loops = prompt("How many times would you like to loop over the list?", "Numbers only, please and thank you");
var sanitized_input = Number(amount_of_loops);
// This is SUPPOSED TO BE the array in which array that holds the final values are held in.
var list_of_outputted_numbers = [];
for (var i = 0; i < sanitized_input; i++) {
// Below I am creating a new array that will contain the a randomized selection of 10 numbers from the original list.
var random_array_of_nums1 = [];
for (var j = 0; j < array_of_nums1.length; j++) {
var rand_NUM_array1 = array_of_nums1[Math.floor(Math.random() * array_of_nums1.length)];
random_array_of_nums1.push(rand_NUM_array1)
}
// Here I'm doing the same thing I did ealier, but with the second list
var random_array_of_nums2 = [];
for (var k = 0; k < array_of_nums2.length; k++) {
var rand_NUM_array2 = array_of_nums2[Math.floor(Math.random() * array_of_nums2.length)];
random_array_of_nums2.push(rand_NUM_array2)
}
// Here I'm finding the total of the randomized first list, than the total of the elements of the randomized second list.
var rand_array_total1 = 0;
for (var l = 0; l < random_array_of_nums1.length; l++) {
rand_array_total1 += random_array_of_nums1[l];
}
var rand_array_total2 = 0;
for (var m = 0; m < random_array_of_nums2.length; m++) {
rand_array_total2 += random_array_of_nums2[m];
}
// Here I am finding the mean of all the numbers of the randomized elements list.
var mean1 = rand_array_total1 / random_array_of_nums1.length;
var mean2 = rand_array_total2 / random_array_of_nums2.length;
// Difference between the means
var mean_diff = mean2 - mean1;
list_of_outputted_numbers.push(mean_diff);
}
console.log(list_of_outputted_numbers);
此代码应能按预期工作。
kooskoos
2019-12-05
小心使用循环变量。您有以下问题:
-
您的第一个
for
循环未声明变量i
;当您的代码在非严格模式下运行时(确实如此),这意味着i
将被定义为全局变量。 -
您在内循环中使用相同的
i
变量;这意味着内循环和外循环将使用相同的i
计数器变量,这不是您想要的。 -
其他时候您使用
var i
;至少您在那里声明了变量,但var
的行为违反直觉,因为变量是 函数作用域 ,而不是 块作用域 。通常您永远不应使用var
;总是首选let
或const
,因为它们带来的意外更少。块范围变量的额外好处是,您可以重复使用相同的变量名称,而无需 覆盖 外部块变量的值(尽管这会让您的代码变得混乱,并且通常最好不要 遮蔽 变量)。
以下是您的循环应有的样子的简化版本:
for (let i = 0; i < sanitized_input; i++) {
// ...
for (let j = 0; j < array_of_nums1.length; j++) {
// ...
}
// ...
for (let j = 0; j < array_of_nums2.length; j++) {
// ...
}
// etc.
}
Jacob
2019-12-05