0

I'm attempting to generate 10 random numbers 3 times and capture the hi, the lo, and last in every 10 number batch. I create an array of the hi/lo/last that gets pushed into another array so I can keep track of the numbers. Unfortunately the push command below seems to be only adding the last hi/lo/last 3 times, instead of each hi/lo/last. Not sure what I'm doing wrong.

Also, I clearly am not an expert programmer having worked mostly with VBA in excel where I could step through a program. Is there any way to do that in Sublime with Javascript? I'm working in a windows environment, using Sublime Text 2, with a Node build for Javascript.

Thanks in advance for the help.

var price; 
var hiPrice; 
var loPrice;
var intervalPrices = []
var initialPrices = [];
var rows = 3;
var columns = 3;

var randomPrices = function(){
    price = 1
    hiPrice = price
    loPrice = price
    for (var i = 1; i <= 30; i++){
        price = Math.round((price+(Math.random()*2-1)/1000)*100000)/100000
        priceIs()
        if (i%10 == 0){
            intervalPrices[0] = hiPrice
            intervalPrices[1] = loPrice
            intervalPrices[2] = price
            initialPrices.push(intervalPrices)
    hiPrice = price
    loPrice = price
        }
    }       
}

var priceIs = function(){
    if (price >= hiPrice) {
        hiPrice = price 
    }
    if (price < loPrice) {
        loPrice = price 
    }
 }

 randomPrices()
 console.log(initialPrices)
dabadaba
  • 9,064
  • 21
  • 85
  • 155

3 Answers3

0

Try making separate object or make it clone instead of original object.

initialPrices.push([intervalPrices[0], intervalPrices[1], intervalPrices[2]]) 

Now

console.log(JSON.stringify(initialPrices))
[[1.00092,0.99835,0.99906],[0.99906,0.99758,0.99762],[0.99907,0.99734,0.99907]] 
Rashmin Javiya
  • 5,173
  • 3
  • 27
  • 49
0

Its just a prob of variable reference whenever you edit intervalPrices it will override prev value of intervalPrices. Pushing same variable with different value will not preserve the value already pushed to the array as every element of that array points to the same intervalPrices variable.

your updated code - read comment for better understanding

var randomPrices = function(){
    price = 1
    hiPrice = price
    loPrice = price
    for (var i = 1; i <= 30; i++){
        //create a new Object
        var obj = {};
        price = Math.round((price+(Math.random()*2-1)/1000)*100000)/100000
        priceIs()
        if (i%10 == 0){
            //assing new values to the Object
            obj[0] = hiPrice
            obj[1] = loPrice
            obj[2] = price
            initialPrices.push(obj)
            hiPrice = price
            loPrice = price
        }
    }       
}
Harpreet Singh
  • 2,651
  • 21
  • 31
0

Using the same variable name to build the array you're trying to push is causing you your issues. Try this instead:

for (var i = 1; i <= 30; i++) {
    price = Math.round((price+(Math.random()*2-1)/1000)*100000)/100000;
    priceIs();
    if (i%10 == 0){
        initialPrices.push([hiPrice,loPrice,price]);
        hiPrice = 1;
        loPrice = 1;
    }
}  

I also set hiPrice and loPrice back to 1 rather than the random number that was generated.

paulccorey
  • 11
  • 2