/    Sign up×
Community /Pin to ProfileBookmark

Need help with an array

[FONT=Tahoma]

[CODE]function sleep (time) {
return new Promise((resolve) => setTimeout(resolve, time));
}

sleep(60000);

a-b=c;
[/CODE]

Having a code.
So every 60 seconds I get a new value of [B]c[/B]. After 5 minutes, I will have a 5 different (or not) values of var [B]c[/B].
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]

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@rootDec 25.2016 — There is no way you can get JavaScript to sleep as it would halt all functions and scripts running on that page until the time out expires.

If you want a function to be called every x milliseconds then you would use an interval event timer to do that for you as it will run in background without hindering the scripts functions.

An interval and a timer will return a reference to it and not the result of that function.

So my take on this is that you want a function to return a value to put in to an array and only keep the last 5 entries.

The question is what have you done so far in the way of coding as you will need to supply something to give people an idea of what you have attempted so far in order to assist you.

When you post any code, use the forum BBCode tags (see my signature for a clue) If you are using JQuery, then please post in the JavaScript Frameworks forum (sub forum to JavaScript) as thats where JQuery questions go.
Copy linkTweet thisAlerts:
@PrestochauthorDec 25.2016 — I connecting to WebSocket and getting from website two values of vars ([B]a[/B] and [B]b[/B]). I need that their difference ([B]a-b[/B]) be recorded in an array and find the lowest and highest ones. With updating.

So I can pick them and do some code later.
Copy linkTweet thisAlerts:
@rootDec 25.2016 — That may be, the issue is that your function above is creating an object and comparing it as a logical evaluation to a system function that returns a handle

  • 1. the object created, you post no code

  • 2. condition logic is... > or < or == or >= or <=

  • 3. no sign of any array


  • consider this bit of nonsense

    x = new Date();
    y = setTimeout("alert('I did it');",1000);
    b = x&gt;=y;
    console.log("&gt;&gt;&gt; x = " + x)
    console.log("&gt;&gt;&gt; y = " + y)
    console.log("&gt;&gt;&gt; x &gt;= y " + b)

    outputs:&gt;&gt;&gt; x = Sun Dec 25 2016 10:22:41 GMT+0000 (GMT)
    <i>&gt;&gt;&gt; </i>y = 2
    <i>&gt;&gt;&gt; </i>x &gt;= y = true


    Which would be no different than the code you posted above except for the =>

    So you need to provide the code you are having a problem with, the method you are obtaining values and so on and how you are storing and the math you are using to compare, add, subtract, etc...

    I you are looking for someone to code this for you, there is a paid help section where you can arrange with a programmer to do the work for you...
    Copy linkTweet thisAlerts:
    @PrestochauthorDec 25.2016 — I would delete the code above but I can't do it. I can create the thread and can't edit it and it's the most stupid thing over here.

    Let's try to explain one more time. Don't look at code, it makes no sense.

    I creating array [CODE]var all=[][/CODE]
    And I have one variable that updating every minute by server. I want that this array will record every value of [B]c[/B] in 5 elements. [CODE]all[0]; all[1]; all[2]; all[3]; all[4][/CODE]
    I can assign a variable to array [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.
    Copy linkTweet thisAlerts:
    @rootDec 25.2016 — 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... 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 ;

    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.
    Copy linkTweet thisAlerts:
    @SempervivumDec 25.2016 — Read

    https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/push

    This code
    [CODE]all[0] = all.push(c);[/CODE]
    adds a new element to the end of the array containing the value of c. And it sets the first element to the length of c. I'm quite shure that this is not what you intend to do.

    When I read this: 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.
    Copy linkTweet thisAlerts:
    @PrestochauthorDec 25.2016 — 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... 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 ;

    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]


    So then how should I add the value of variable that I getting to myArray? [B]myArray = myArray.push(c);[/B] should not work. Where [CODE]c = (a-b);[/CODE]
    Copy linkTweet thisAlerts:
    @rootDec 26.2016 — Putting in to an array is [B]myArray.push( value ) [/B] and NOT [B]myArray = myArray.push( value )[/B] what you are doung if you [B]myArray = myArray.push( value );[/B] is putting something in to the array and then assigning the length of the array to myArray variable, thus destroying the array.

    if the value [B]c[/B] is what you need to put in to the array, its myArray.push(c) which would filll up the array sequentially until the process stops, if you use the prototype function I have illustrated [B]myArray.onlyFive( c );[/B] would push any number of values in to the myArray array but it would only ever have 5 values in it.

    To make it clear, the elements I have illustrated are to demonstrate methods of achieving various goals but you still have to fully demonstrate what code you have built or explained what it is you are trying to attempt. To get help you need to show people what it is you have done so far and explain what it is you are trying to do and explain the expected outcome as well as any errors you have encountered.

    We are basically stabbing in the dark here and you need to help with the full code you are currently using.
    Copy linkTweet thisAlerts:
    @PrestochauthorDec 26.2016 — [B]myArray.onlyFive( c );[/B] [/QUOTE]
    This works but it doesn't store the previous values. If I will write this line 5 times [CODE]myArray.onlyFive( c ); myArray.onlyFive( c ); myArray.onlyFive( c ); myArray.onlyFive( c ); myArray.onlyFive( c );[/CODE]
    I will get 5 same values every time I get it. And when I will get another value of [B]c[/B], I again will get the 5 same values. I wanna do like
    [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]
    Copy linkTweet thisAlerts:
    @rootDec 26.2016 — Your post said 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.

    You don't seem to be comprehending the use of the prototype function, using it 5 times in a row would store the same value, I merely demonstrated how it can store a set limit of numbers.

    You still haven show any code of your own, so until you can provide this to show what it is your attempting to do, it is pointless continually shooting in the dark here and you need to be more specific in how you word what you are trying to do..
    Copy linkTweet thisAlerts:
    @c39261216Dec 27.2016 — THIẾT KẾ WEB Táº*I "Thiet Ke Web Chuyen .Com" THEO YÊU CẦU TRỌN GÓI CHỈ 1.990.000 VND - 0934 150 770 (VIBER) - 0978 106 552 (ZALO)(Mr. HoÃ*ng)

    Trang web sẽ thiết kế giao diện theo yêu cầu riêng, hợp sở thÃ*ch, phong thủy, váº*n mệnh… để giói thiệu sản phẩm, dịch vụ, công ty, cá»*a hÃ*ng … để bạn phát triển kinh doanh không ngừng.

    Trang web sẽ được bảo hÃ*nh vÄ©nh viá»…n.

    Hãy liên hệ thông tin bên dÆ°á»›i ngay ( không bình luáº*n thắc mắc vÃ*o bÃ*i viết nÃ*y vì mình không xem lại tin nhắn tại đây)

  • + 0934 150 770 (VIBER) - 0978 1065 52 (ZALO)(Mr. HoÃ*ng) ( bạn có thể nhắn tin để yêu cầu gọi lại miá»…n phÃ*)


  • + Email: hohoanganh205@ yahoo. com - SKYPE: hohoanganh205


  • + website: "Thiet Ke Web Chuyen .Com"


  • Có thể bạn quan tâm: thiet ke web gia re , thiet ke web re dep , cong ty thiet ke web ,thiet ke web tron goi ,thiet ke web theo yeu cau
    ×

    Success!

    Help @Prestoch spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...