Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9323553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:22:22+00:00 2026-06-19T04:22:22+00:00

For some reason this javascript doesn’t seem to calculate properly and keeps giving me

  • 0

For some reason this javascript doesn’t seem to calculate properly and keeps giving me infinity for some array values.

var stock = {
        "data": {
            "portfolio": [
                [1298641715000, 1631.00],
                [1298656616000, 1311.00],
                [1299606578000, 1311.00],
                [1300381305000, 820.50],
                [1300813418000, 2074.80],
                [1300970915000, 3862.50],
                [1300985378000, 4597.35],
                [1301071778000, 3554.85],
                [1301505492000, 5653.05],
                [1302195878000, 4871.80],
                [1302368766000, 4079.80],
                [1306343616000, 5062.60],
                [1307036495000, 5062.60],
                [1307726770000, 7207.60],
                [1307727695000, 6372.60],
                [1312998095000, 5816.10],
                [1313430095000, 5816.10],
                [1316625492000, 7816.10],
                [1316711892000, 6566.10],
                [1316713295000, 7777.10],
                [1316713295000, 8921.60],
                [1316799695000, 10902.10],
                [1316886095000, 8627.10],
                [1318094770000, 7717.10],
                [1318267163000, 6807.10],
                [1326306370000, 8228.30],
                [1327429500000, 9228.30],
                [1328208664000, 10222.05],
                [1329503831000, 10188.65],
                [1330368095000, 11274.15],
                [1330541464000, 12006.75],
                [1330714264000, 13222.25],
                [1331059408000, 14029.75],
                [1331144806000, 15676.55],
                [1333648264000, 17256.02],
                [1334080424000, 18701.62],
                [1336672860000, 19745.42],
                [1336758824000, 20018.77],
                [1336773969000, 21130.77],
                [1337015320000, 20312.47],
                [1337277827000, 21650.02],
                [1339002758000, 22285.97],
                [1341934145000, 23972.67],
                [1341934243000, 24182.67],
                [1342103859000, 25830.67],
                [1342475250000, 25380.67],
                [1342654950000, 25669.09],
                [1343147542000, 25797.43],
                [1343412475000, 25994.43],
                [1343658668000, 26203.43],
                [1343804523000, 26342.43],
                [1343840755000, 26432.43],
                [1343922704000, 25914.43],
                [1343922759000, 26031.43],
                [1345053816000, 26156.43],
                [1345138832000, 26253.43],
                [1345576479000, 26483.43],
                [1345576537000, 26588.43],
                [1345656708000, 26628.43],
                [1346442006000, 26704.43],
                [1346953652000, 26456.43],
                [1347553896000, 26586.43],
                [1347554286000, 26865.43],
                [1347559351000, 27062.43],
                [1347560040000, 27533.43],
                [1347560369000, 27351.43],
                [1347641968000, 26577.43],
                [1348063653000, 26860.43],
                [1348591198000, 26588.43],
                [1348591500000, 26598.43],
                [1348668767000, 26728.43],
                [1348681823000, 27094.43],
                [1349367062000, 27254.43],
                [1349368159000, 27379.43],
                [1350310748000, 27675.43],
                [1350503445000, 27858.43],
                [1350923279000, 27979.43],
                [1352131960000, 27742.43],
                [1353522221000, 27997.43],
                [1353685832000, 28299.43],
                [1355157961000, 28545.43],
                [1355766797000, 28692.43],
                [1355947837000, 28913.43],
                [1357671266000, 29304.43],
                [1357672054000, 29531.44],
                [1358448645000, 28856.44],
                [1358448738000, 29036.44],
                [1358455579000, 29236.44],
                [1358540610000, 29430.44],
                [1360095697000, 30077.44],
                [1360596277000, 30154.44],
                [1360612771000, 30651.44],
                [1360860220000, 30789.44],
                [1360869993000, 31389.44]
            ],
                "portfolio2": [
                [1298641715000, 1631.00],
                [1298656616000, 1311.00],
                [1299606578000, 1311.00],
                [1300381305000, 820.50],
                [1300813418000, 2074.80],
                [1300970915000, 3862.50],
                [1300985378000, 4597.35],
                [1301071778000, 3554.85],
                [1301505492000, 5653.05],
                [1302195878000, 4871.80],
                [1302368766000, 4079.80],
                [1306343616000, 5062.60],
                [1307036495000, 5062.60],
                [1307726770000, 7207.60],
                [1307727695000, 6372.60],
                [1312998095000, 5816.10],
                [1313430095000, 5816.10],
                [1316625492000, 7816.10],
                [1316711892000, 6566.10],
                [1316713295000, 7777.10],
                [1316713295000, 8921.60],
                [1316799695000, 10902.10],
                [1316886095000, 8627.10],
                [1318094770000, 7717.10],
                [1318267163000, 6807.10],
                [1326306370000, 8228.30],
                [1327429500000, 9228.30],
                [1328208664000, 10222.05],
                [1329503831000, 10188.65],
                [1330368095000, 11274.15],
                [1330541464000, 12006.75],
                [1330714264000, 13222.25],
                [1331059408000, 14029.75],
                [1331144806000, 15676.55],
                [1333648264000, 17256.02],
                [1334080424000, 18701.62],
                [1336672860000, 19745.42],
                [1336758824000, 20018.77],
                [1336773969000, 21130.77],
                [1337015320000, 20312.47],
                [1337277827000, 21650.02],
                [1339002758000, 22285.97],
                [1341934145000, 23972.67],
                [1341934243000, 24182.67],
                [1342103859000, 25830.67],
                [1342475250000, 25380.67],
                [1342654950000, 25669.09],
                [1343147542000, 25797.43],
                [1343412475000, 25994.43],
                [1343658668000, 26203.43],
                [1343804523000, 26342.43],
                [1343840755000, 26432.43],
                [1343922704000, 25914.43],
                [1343922759000, 26031.43],
                [1345053816000, 26156.43],
                [1345138832000, 26253.43],
                [1345576479000, 26483.43],
                [1345576537000, 26588.43],
                [1345656708000, 26628.43],
                [1346442006000, 26704.43],
                [1346953652000, 26456.43],
                [1347553896000, 26586.43],
                [1347554286000, 26865.43],
                [1347559351000, 27062.43],
                [1347560040000, 27533.43],
                [1347560369000, 27351.43],
                [1347641968000, 26577.43],
                [1348063653000, 26860.43],
                [1348591198000, 26588.43],
                [1348591500000, 26598.43],
                [1348668767000, 26728.43],
                [1348681823000, 27094.43],
                [1349367062000, 27254.43],
                [1349368159000, 27379.43],
                [1350310748000, 27675.43],
                [1350503445000, 27858.43],
                [1350923279000, 27979.43],
                [1352131960000, 27742.43],
                [1353522221000, 27997.43],
                [1353685832000, 28299.43],
                [1355157961000, 28545.43],
                [1355766797000, 28692.43],
                [1355947837000, 28913.43],
                [1357671266000, 29304.43],
                [1357672054000, 29531.44],
                [1358448645000, 28856.44],
                [1358448738000, 29036.44],
                [1358455579000, 29236.44],
                [1358540610000, 29430.44],
                [1360095697000, 30077.44],
                [1360596277000, 30154.44],
                [1360612771000, 30651.44],
                [1360860220000, 30789.44],
                [1360869993000, 31389.44]
            ],
                "standard": []
        }
    };

    var stockbase = stock.data.portfolio;
    var stock1 = stock.data.portfolio2;

    for (i = 0; i < (stock.data.portfolio2.length); i++) {
        stock.data.portfolio2[i][1] = stock.data.portfolio2[i][1] * Math.random();
    }

    //var performance1 = stock.data.portfolio2;

    var i = 0;

    while(i<stockbase.length)
    {
            if (i == 0){
            stock1[i][0] = stockbase[i][0];
            stock1[i][1] = 0;
        } else if (stockbase[i][1] > stockbase[(i - 1)][1]) {
            stock1[i][0] = stockbase[i][0];
            stock1[i][1] = (stockbase[i][1]) / (stockbase[(i - 1)][1]);
            stock1[i][1] -= 1;
            stock1[i][1] *= 100;
        } else if (stockbase[i][1] < stockbase[(i - 1)][1]) {
            stock1[i][0] = stockbase[i][0];
            stock1[i][1] = ((stockbase[i][1]) / (stockbase[(i-1)][1]));
            stock1[i][1] -= 1;
            stock1[i][1] *= 100;
        } else {
            stock1[i][0] = stockbase[i][0];
            stock1[i][1] = 0;
        }
        i++;
    }

    //console.log(stockbase);
    //console.log(stock1);

    var stockbase = stock.data.portfolio2;
    var stock2 = stock.data.portfolio;

    i = 0;

    while(i<stockbase.length)
    {
            if (i == 0){
            stock2[i][0] = stockbase[i][0];
            stock2[i][1] = 0;
        } else if (stockbase[i][1] > stockbase[(i - 1)][1]) {
            stock2[i][0] = stockbase[i][0];
            stock2[i][1] = (stockbase[i][1]) / (stockbase[(i - 1)][1]);
            stock2[i][1] -= 1;
            stock2[i][1] *= 100;
        } else if (stockbase[i][1] < stockbase[(i - 1)][1]) {
            stock2[i][0] = stockbase[i][0];
            stock2[i][1] = ((stockbase[i][1]) / (stockbase[(i-1)][1]));
            stock2[i][1] -= 1;
            stock2[i][1] *= 100;
        } else {
            stock2[i][0] = stockbase[i][0];
            stock2[i][1] = 0;
        }
        i++;
    }

    console.log(stockbase);
    console.log(stockbase==stock.data.portfolio2);

the variable stock 1 works perfectly after the loop without any problems. Interesting part is after the first while loop, stock.data.portfolio2 has the same array values as stock1 does even though no changes are supposed to be made to stock.data.portfolio2.

I’m confused on what I’m doing wrong.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T04:22:24+00:00Added an answer on June 19, 2026 at 4:22 am

    When doing the following:

    var stock1 = stock.data.portfolio2;

    you are assigning stock1 to a reference of the array stock.data.portfolio2. That means all changes made to stock.data.portfolio2 will also be seen in stock1. Since stock.data.portfolio2 is an array of arrays I would recommend creating a function to copy that data for you into your variable stock1

    function copyPortfolio(arr) {
        var s = [];
        for(i = 0; i < (arr.length); i++) { s.push(arr[i].slice()); }
        return s;
    }
    
    var stock1 = copyPortfolio(stock.data.portfolio2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

for some reason this code wont work, it doesn't seem to read the javascript.php
For some reason, this line of code is returning undefined for $(this).attr(href) $(a).attr(href, javascript:page('
For some reason this line of code is giving me quite a problem. struct
This used to work, but for some reason it doesn't anymore. I use a
For some reason this code doesn't work in Safari anyone have any clue to
For some reason the following line of javascript is giving me an error object
I am running some JavaScript code in Firefox that doesn't seem to be following
For some reason this isn't working, am I missing something obvious? RewriteRule ^(.*)infopopup.html$ /acatalog/infopopup.html
For some reason this () character shows up in Firefox 10 randomly throughout my
For some reason this statement is working fine: vms.Where(vm => vm.MessageType == ValidationMessage.EnumValidationMessageType.Warning) But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.