gpt4 book ai didi

javascript - 无法从循环中获取值以推送到循环外的数组

转载 作者:行者123 更新时间:2023-11-30 19:03:50 24 4
gpt4 key购买 nike

我创建了一种方法来获取新的随机值列表,找到所述列表的平均值,然后从以相同方式使用不同数字列表创建的不同数字中减去该数字。我想让这个循环循环一定次数,但它只会在列表中循环一次。

这里的问题是 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);

最佳答案

您正在使用相同的 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);

此代码应按预期工作。

关于javascript - 无法从循环中获取值以推送到循环外的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59203328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com