I need to populate three arrays with different values for the same range.
For ex:
Have an array like this
var arr = [1,2,3,4,5,6];
i need this array in format
a[0]=[2,3,4,1,5];
a[1]=[4,3,2,1,5];
a[2]=[2,3,1,5,4];
so three indexes of array show three different array values in order.
I am trying with this below snippets
(function($){
$.fn.shuffle = function() {
return this.each(function(){
var items = $(this).children();
return (items.length)
? $(this).html($.shuffle(items))
: this;
});
}
$.shuffle = function(arr) {
for(
var j, x, i = arr.length; i;
j = parseInt(Math.random() * i),
x = arr[--i], arr[i] = arr[j], arr[j] = x
);
return arr;
}
})(jQuery);
var arr = [1,2,3,4,5,6];
a =[];
b=[];
c=[];
a = $.shuffle(arr);
b = $.shuffle(a);
c = $.shuffle(b);
document.write(a);
document.write(b);
document.write(c);
Related
Function that compares and array with its numerically sorted version in ascending order. Return the sum of the square differences between each term of the two arrays.
Expample:
For the array a= [2,3,1], the sorted version is b=[1,2,3],
.the sum of the square differences is (2-1)2 + (3-2)2 + (1-3)2 = 6
. or in terms of array elements (a[0]-b[0])2 + (a[1]-b[1])2 + (a[2]+b[2])2 = 6
const arr = [2, 3, 1];
function myFunction(arr) {
var newarr = [];
var a = arr;
var b = arr;
var sum = 0;
b.sort(function(a, b) {
return a - b
});
for (var i = 0; i < b.length; i++) {
var sub = a[i] - b[i];
sub = sub * 2;
sum += sub;
newarr.push(sum);
}
return newarr;
}
console.log(myFunction(arr));
Assigning arrays doesn't make copies. So a, b, and arr are all the same array, and you're just subtracting a number from itself, always getting zero. You need to make a copy of the array in the other variable.
You're also not squaring correctly, it should be sub * sub, not sub * 2.
You're supposed to return sum. There's no need for newArr.
const arr = [2, 3, 1];
function myFunction(arr) {
var sorted = [...arr]; // make copy
sorted.sort(function(a, b) {
return a - b
});
var sum = 0;
for (var i = 0; i < sorted.length; i++) {
var sub = arr[i] - sorted[i];
sub = sub * sub;
sum += sub;
}
return sum;
}
console.log(myFunction(arr));
I have data file (php) with 24 blocks of data, structured as below.
//ts1
$P[] = [0];
$S[] = [[1,95.8172406762736],[2,104.97526726371],[3,112.03839938973],[4,101.70457396977],];
$F[] = [];
$FB[] = [];
//ts2
$P[] = [0];
$S[] = [[1,103.190939795922],[2,105.297198378469],[3,105.829786652111]];
$F[] = [];
$FB[] = [];
//ts3
$P[] = [0];
$S[] = [[1,107.285278217373],[2,103.557795069809],[3,105.686758569246],[4,103.748341353355]];
$F[] = [];
$FB[] = [];
I need to shuffle the first 12 blocks and then shuffle block 13-24. The code I have does not appear to work as it still shuffles all 24 blocks at once. I'm not sure how it should be written otherwise..
// DATA INITIALISATION
// - Reads all data sets from server
// - Generates list of objects
// - Randomises list of objects
function DataInit1()
{
SeriesList = [];
CurrentGraph.Series = 0;
// load all the data sets from server
$.getJSON("datademo.php",
function(Data1)
{
for(var i=0; i<Data1.length; i+=4)
{
var P = Data1[i+0];
var S = Data1[i+1];
var F = Data1[i+2];
var FB = Data1[i+3];
var NewSeries = new SeriesClass(P,S,F,FB);
NewSeries.SeriesNumber = (i/4)+1;
SeriesList.push(NewSeries);
}
}
);
// shuffle each of the series lists to a random order
s1 = SeriesList.length/2;
s2 = SeriesList.length;
for(var i=0; i<s1; i++)
{
var j = Math.floor(Math.random() * (i+1));
var x = SeriesList[i];
SeriesList[i] = SeriesList[j];
SeriesList[j] = x;
}
for(var i=s1; i<s2; i++)
{
var j = Math.floor(Math.random() * (i+1));
var x = SeriesList[i];
SeriesList[i] = SeriesList[j];
SeriesList[j] = x;
}
}
edit: I've changed it now to the following (not pretty but I don't have time to tidy it). Now it randomizes series 1-12, but series 13-24 is now not randomized. I don't code that often and I can't see why it would work in the first bit but not the second one.
// shuffle the series list to a random order
for(var i=SeriesList.length-13; i>0; i--)
{
var j = Math.floor(Math.random() * (i+1));
var x = SeriesList[i];
SeriesList[i] = SeriesList[j];
SeriesList[j] = x;
}
for(var i={from: 12, to: 23}; i>0; i--)
{
var j = Math.floor(Math.random() * (i+1));
var x = SeriesList[i];
SeriesList[i] = SeriesList[j];
SeriesList[j] = x;
}
I think the easiest way is to break it into two arrays, shuffle them separately, then push them back together. I've used the shuffle method from this question: How can I shuffle an array?, and the method of joining arrays from How to extend an existing JavaScript array with another array, without creating a new array?
See the fiddle at https://jsfiddle.net/u25ta64x/1/
var SeriesList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26];
var firstHalf = SeriesList.slice(0,13);
var secondHalf = SeriesList.slice(13,26);
function shuffle(a) {
var j, x, i;
for (i = a.length; i; i--) {
j = Math.floor(Math.random() * i);
x = a[i - 1];
a[i - 1] = a[j];
a[j] = x;
}
}
shuffle(firstHalf);
shuffle(secondHalf)
var shuffledList = firstHalf;
Array.prototype.push.apply(shuffledList, secondHalf);
console.log(shuffledList);
I've looked at some other questions similar to this, but I'm getting my array in a unique way and I can't figure out for the life of my how to change it to a 2D array.
//Special function for adding arrays, just use sumArray on first array with second array in parenthesis
//==========================================
Array.prototype.sumArray = function (arr) {
var sum = this.map(function (num, idx) {
return num + arr[idx];
});
return sum;
}
var array1 = [1,2,3,4];
var array2 = [5,6,7,8];
var sum = array1.sumArray(array2);
Logger.log("sum: " + sum);
//==========================================
var calc = ss.getRangeByName( "calc" );
var target = ss.getRangeByName( "target" );
var current = ss.getRangeByName( "current" );
var left = ss.getRangeByName( "left" );
var gainedEVs = calc.getValues();
var goalEVs = target.getValues();
var oldEVs = current.getValues();
var leftEVs = left.getValues();
//Make everything ints
//==========================================
for(var i = 0; i < oldEVs.length; i++) {
Logger.log(oldEVs.length);
oldEVs[i] = parseInt(oldEVs[i]);
}
for(var i = 0; i < gainedEVs.length; i++) {
gainedEVs[i] = parseInt(gainedEVs[i]);
}
for(var i = 0; i < goalEVs.length; i++) {
goalEVs[i] = parseInt(goalEVs[i]);
}
for(var i = 0; i < leftEVs.length; i++) {
leftEVs[i] = parseInt(leftEVs[i]);
}
//==========================================
var newEVs = [[oldEVs.sumArray(gainedEVs)]];
var newLeft = [[goalEVs.subArray(newEVs)]];
//Now I try to set values and I get the error
current.setValues(newEVs);
I've tried changing the setValues to setValues([newEVs]); but that doesn't work either. Any clue on how I can get my array of newEVs to be the correct height? It has the right number of values, but those values are being stored in columns, not rows. (in this case all of my ranges are 6 rows 1 col)
Since your ranges are small, you don't have to worry too much about performance, so you can convert them from rows to columns using a loop:
var column = [];
for (var i=0; i<newEVs.length; i++){
column.push([newEVs[i]]);
}
current.setValues(column);
What I like is an Array like this
myArray = [
[{"value":1},{"value":2},{"value":3}]
[{"value":2},{"value":4},{"value":6}]
]
I try to use two loops to build it. The outer loop shout build the two "outer" Elements in Array, the inner loop shout build the 3 inner objects.
http://jsfiddle.net/xtrem1234/xx06zt5s/
var dataset = [];
var categories = ["Category_1", "Category_2"];
var myArray = [];
categories.forEach(function (category, index) {
for (var n = 1; n < 4; n++) {
var d;
d = {};
d.value = (index+1) * n;
console.log("index: " + index);
console.log("n: " + n);
console.log("d.value: " + d.value);
dataset[n] = d;
}
myArray.push(dataset);
});
console.log(JSON.stringify(myArray));
The issue is, your array dataset is a global array. Define it inside forEach loop.
Code
var categories = ["Category_1", "Category_2"];
var myArray = [];
categories.forEach(function(category, index) {
var dataset = [];
for (var n = 1; n < 4; n++) {
var d;
d = {};
d.value = (index + 1) * n;
dataset[n] = d;
}
myArray.push(dataset);
});
console.log(JSON.stringify(myArray));
A: you need to reinitialize the dataset array each time the forEach loop runs
B: you can just push() the info to the dataset array, the same way you do for myArray
var categories = ["Category_1", "Category_2"];
var myArray = [];
categories.forEach(function (category, index) {
var dataset = [];
for (var n = 1; n < 4; n++) {
dataset.push({'value': (index+1) * n});
}
myArray.push(dataset);
});
console.log(JSON.stringify(myArray));
I want to create an array like this:
s1 = [[[2011-12-02, 3],[2011-12-05,3],[5,13.1],[2011-12-07,2]]];
How to create it using a for loop? I have another array that contains the values as
2011-12-02,3,2011-12-05,3,2011-12-07,2
One of possible solutions:
var input = ['2011-12-02',3,'2011-12-05',3,'2011-12-07',2]
//or: var input = '2011-12-02,3,2011-12-05,3,2011-12-07,2'.split(",");
var output = [];
for(i = 0; i < input.length; i += 2) {
output.push([t[i], t[i + 1]])
}
If your values always come in pairs:
var str = '2011-12-02,3,2011-12-05,3,2011-12-07,2',//if you start with a string then you can split it into an array by the commas
arr = str.split(','),
len = arr.length,
out = [];
for (var i = 0; i < len; i+=2) {
out.push([[arr[i]], arr[(i + 1)]]);
}
The out variable is an array in the format you requested.
Here is a jsfiddle: http://jsfiddle.net/Hj6Eh/
var s1 = [];
for (x = 0, y = something.length; x < y; x++) {
var arr = [];
arr[0] = something[x].date;
arr[1] = something[x].otherVal;
s1.push(arr);
}
I've guessed here that the date and the other numerical value are properties of some other object, but that needn't be the case...
I think you want to create an array which holds a set of arrays.
var myArray = [];
for(var i=0; i<100;i++){
myArray.push([2011-12-02, 3]); // The values inside push should be dynamic as per your requirement
}