kuthulu
kuthulu

Reputation: 21

Can't get values from loop to push to array outside of loop

I created a way to get a new list of randomized values, find the mean of said list, then subtract that from a different number that was created in the same way with a different list of numbers. I want to have this loop over a set number of times, but it will only loop over the list once.

The issue here is that the list_of_outputted_numbers will only push mean_diff once, and not the specified number of times.

// 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);

Upvotes: 1

Views: 298

Answers (2)

Jacob
Jacob

Reputation: 78850

Be careful with loop variables. You have the following issues:

  1. Your first for loop doesn't declare the variable i; when your code runs in non-strict mode (it is), that means that i will be defined as a global variable.
  2. You're using the same i variable inside of inner loops; that means the same i counter variable will be used for both the inner and outer loops, not what you want.
  3. Other times you're using var i; at least there you're declaring the variable, but var behaves counter-intuitively, in that the variable is function scoped, not block scoped. Generally you should never use var; always prefer let or const since those have less surprises in store. An added bonus to blocked-scoped variables is you can reuse the same variable names without overwriting the value of outer block variables (though that makes your code confusing and it's generally better to not shadow variables).

Here's an abbreviated version of how your loops should look:

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.
}

Upvotes: 0

kooskoos
kooskoos

Reputation: 4859

You are using the same i variable to control your loops so i is exceeding sanitized_input in first iteration only.

// 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);

This code should work as expected.

Upvotes: 1

Related Questions