[FONT=Tahoma]
[CODE]function sleep (time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
sleep(60000);
a-b=c;
Having a code.
So every 60 seconds I get a new value of [B]c[B]c
I wanna add all these values in array and find the lowest and highest ones. And next, on 6th minute, when I will get another value, compare it with previous fives. And if this value will be higher than highest from the previous fives, or lower than lowest, choose it then.
If this value will be in between previous fives, than add it in that array as a last and clear the first one. [/FONT]
sleep
x = new Date();
y = setTimeout("alert('I did it');",1000);
b = x>=y;
console.log(">>> x = " + x)
console.log(">>> y = " + y)
console.log(">>> x >= y " + b)
>>> x = Sun Dec 25 2016 10:22:41 GMT+0000 (GMT)
<i>>>> </i>y = 2
<i>>>> </i>x >= y = true
[CODE]var all=[][/CODE]
[CODE]all[0]; all[1]; all[2]; all[3]; all[4][/CODE]
[CODE] all[0] = all.push(c);[/CODE]
. But it will clear [B]all[0][/B] every time I will get a new value. I need to store the value of [B]c[/B] in [B]all[0][/B]. And next time when I will get another [B]c[/B], assign it to [B]all[1][/B] and store the first value in [B]all[0][/B]. Thus till 5th step. Constantly keeping the previous value of [B]c[/B] in storage.Array.prototype.onlyFive = function( val ){
var len = this.length;
if( len == 5) this.shift(); // we need to remove first element from the array
this.push( val );
}
// use
myArray = [];
myArray.onlyFive( 1 );
myArray.onlyFive( 2 );
myArray.onlyFive( 3 );
myArray.onlyFive( 4 );
myArray.onlyFive( 5 );
myArray.onlyFive( 6 );
myArray.onlyFive( 7 );
console.log( myArray.length +" -- " + myArray.toString() );
it is also good practice to get in to the habbit of terminating your lines wehre they should be terminated with a semi-colon ;
[CODE]all[0] = all.push(c);[/CODE]
I need to store the value of c in all[0]. And next time when I will get another c, assign it to all[1] and store the first value in all[0]. Thus till 5th step. Constantly keeping the previous value of c in storage.[/QUOTE]I assume that this code
[CODE]if (all.length < 5) all.push(c);[/CODE]
should do what you want.You assign an element to an array with [b]arrayName.push( valueToStore );[/b]
As well as that [B]all[/B] is not a good name to call an array element as it features in the JavScript keywords list. (http://www.javascripter.net/faq/reserved.htm )
What you need is a function that will automatically trim the array to only 5 elements, so something like...it is also good practice to get in to the habbit of terminating your lines wehre they should be terminated with a semi-colon
Array.prototype.onlyFive = function( val ){
var len = this.length;
if( len == 5) this.shift(); // we need to remove first element from the array
this.push( val );
}
// use
myArray = [];
myArray.onlyFive( 1 );
myArray.onlyFive( 2 );
myArray.onlyFive( 3 );
myArray.onlyFive( 4 );
myArray.onlyFive( 5 );
myArray.onlyFive( 6 );
myArray.onlyFive( 7 );
console.log( myArray.length +" -- " + myArray.toString() );
;
In the console the output of the above script would be
5 -- 3,4,5,6,7 where 5 is the length and then the elements of the array displayed as a CSV string.[/QUOTE]
[CODE]c = (a-b);[/CODE]
[B]myArray.onlyFive( c );[/B] [/QUOTE]
[CODE]myArray.onlyFive( c ); myArray.onlyFive( c ); myArray.onlyFive( c ); myArray.onlyFive( c ); myArray.onlyFive( c );[/CODE]
[CODE]
// Getting value of c every minute
//1st step. I got value that is "3". Result should be
[B]5 -- 3,undefined,undefined,undefined,undefined[/B]
//2nd step. I got value that is "8".
[B]5 -- 3, 8, undefined, undefined, undefined[/B]
//3rd step. I got value that is "12".
[B]5 -- 3, 8, 12, undefined, undefined[/B]
//4th step. I got value that is "30".
[B]5 -- 3, 8, 12, 30, undefined[/B]
//5th step. I got a value that is "6"
[B]5 -- 3, 8, 12, 30, 6[/B]
//6th step. I got value that is "4"
[B]5 -- 8, 12, 30, 6, 4[/B][/CODE]
I will have a 5 different (or not) values of var c.[/quote]so I assumed from that you only need 5 values, if you need a different number then modify the function.
0.1.9 — BETA 5.18