Table not displaying correct answer. What am I doing wrong?

Table not displaying correct answer. What am I doing wrong?

PWHPWH Posts: 26Questions: 5Answers: 0

The code for my table is here http://live.datatables.net/jofuhaca/13/edit It runs on my pc but not on the live link, not sure why.
What I am trying to do, is collect api data on a loop and look for a difference in the 'price' object.
The code all runs on my pc except it appears to be calculating and displaying one row out. Console.log shows..

i 0 maintst.js:45:16
L 0.00000249  M 0.00000249  D -0.00000249 maintst.js:46:16
i 1 maintst.js:45:16
L 0.00001957  M 0.00001957  D -0.00001708 maintst.js:46:16
i 2 maintst.js:45:16
L 0.00000062  M 0.00000062  D 0.00001895 maintst.js:46:16
i 3 maintst.js:45:16
L 0.00000584  M 0.00000584  D -0.00000522 maintst.js:46:16
i 4 maintst.js:45:16
L 0.00000001  M 0.00000001  D 0.00000583

i is the counter, L is the lastPrice value M is the current price and D is the difference. What I am after using the first line as an example is for D to equal zero, what is happening is .00000249 is being subtracted from L (or M) 0.00001957 on the next iteration, (i =1) to give -0.00001708 when that too should be zero. I cannot fathom why this is happening. As previously mentioned, I am very new to this so have probably missed something very fundamental.
Thank you for your help

Replies

  • kthorngrenkthorngren Posts: 8,880Questions: 25Answers: 2,022

    It runs on my pc but not on the live link, not sure why.

    The console shows this error.

    Uncaught ReferenceError: markets is not defined

    Maybe fixing that will help the test case to run.

    Not sure just by looking at the code what is happening. See if you can get it running so we can take a look.

    Kevin

  • PWHPWH Posts: 26Questions: 5Answers: 0

    I commented out the 'function markets' line and } and its working now!
    http://live.datatables.net/jofuhaca/15/edit
    How did you see the uncaught reference error?

  • kthorngrenkthorngren Posts: 8,880Questions: 25Answers: 2,022

    The error was in the browser's console. Still getting an error:

    Uncaught ReferenceError: markets is not defined

    Made a couple changes. One recommendation is to use a variable to convert your number once instead of doing the same conversion multiple times. The code is more efficient this way, for example: var market_price = parseFloat(market.price);

    Also remove the toFixed() from the calculations. The toFixed() method returns a string value which you don't want. Only use this to format the output.

    I added a couple console.log statements so you can see better what is happening The first time through the loop lastPrice = 0 and market_price = 0.00000249 then you use difference = lastPrice - market_price;. difference is going to be -0.00000249 as you mentioned but you are expecting it to be 0. Not understanding your data and what you are expecting to do with it its hard to say what you should do. Feel free to ask questions but this portion of your code is outside of Datatables support.

    Also I found you have statements like this: lastPrice = [Number(market.price).toFixed(8)];. The [] indicate an array. Not sure why you have them but I removed them in my example.

    Kevin

  • PWHPWH Posts: 26Questions: 5Answers: 0

    Thank you Kevin for your advice. I have sorted out the initialisation/alignment problem and am just left with how to display the result in the table. In the live link below, monitoring console.log, you will see that first time round the Lastprice variable is not set and so the D answer is NaN, I understand that is expected. Example:

    i 74 L undefined  M 0.00000001  D NaN maintst.js:46:17
    i 75 L undefined  M 0.00000046  D NaN maintst.js:46:17
    i 76 L undefined  M 0.00000557  D NaN maintst.js:46:17
    i 77 L undefined  M 0.00000465  D NaN 
    

    Then, as the table loops, the L variable is updated and you will see if you monitor console.log that eventually the calculation is performed. Example:

    do nothing, prices are unchanged 78 maintst.js:48:17
    do nothing, prices are unchanged 78 maintst.js:48:17
    do nothing, prices are unchanged 36 maintst.js:48:17
    i 36 L 0.00001284  M 0.00001262  D -0.00000022 maintst.js//**price changed here**
    do nothing, prices are unchanged 41 maintst.js:48:17
    do nothing, prices are unchanged 3 maintst.js:48:17
    i 3 L 0.0000062  M 0.00000631  D 0.00000011//**price changed here**
    

    However, the result is not displayed in the table. For clarity (I hope) I have set the table code to

    { data: 'initialprice'},
                { data: difference},
                //{ data: null, render: function ( data, type, row ) {
                // return Math.round( ( row.price - row.difference ) / row.price * 100 )+'%';} 
                //}
                ],
    

    which displays object Object, which I think means the table does not know which object in 'difference' to use. Ultimately, I want to use the commented out code above which returns NaN.
    What should I do? Thank you

    http://live.datatables.net/jofuhaca/18/edit

  • kthorngrenkthorngren Posts: 8,880Questions: 25Answers: 2,022
    edited February 19

    displays object Object

    The difference variable is an object which is why you see this in the table. You need to specify the specific data from the object you want to display. Since the key is the name you will use something like this:

           { data: null,
                  render: function (data, type, row) {
                    return difference[data.name];
                  }
                },
    

    Not sure if this is what you want but I added this code to initialize lastPrice[coin_name] at 0:

                      if ( !lastPrice.hasOwnProperty(coin_name)) {
                        lastPrice[coin_name] = 0.0;
                      }
    

    http://live.datatables.net/bihijahe/2/edit

    Kevin

  • PWHPWH Posts: 26Questions: 5Answers: 0

    Thank you again Kevin for your invaluable help. With it, I was able to work out exactly what I wanted which was

    { data: null,
                    render: function ( data, type, row ) {
                    return parseFloat( difference[data.name] / ( difference[data.name] + row.price * 1)  * 100 ).toFixed(2) +'%';
                    }
                }
    

    Also for showing me how to initialise the data object. I had looked for hours on the net without finding the solution. The unintentional consequence of doing the initialisation to zero is, on startup, the above code results in 50% in the column which is logically correct. Ideally, lastprice should be initialised with 'price' to give zero in the column on startup but it seems this will take a bit of working out.
    Once again thanks for all your help. It is appreciated.
    I know a lot more than I did a few weeks ago.

  • kthorngrenkthorngren Posts: 8,880Questions: 25Answers: 2,022

    Good, glad you got it worked out.

    Kevin

Sign In or Register to comment.