0

I am building a dynamic svg calendar bar. In order to calculate how many divisions the bar needs (based in months) I divide the total width of the bar by the date diff in months to figure out how many pixels should be in each division.

Basic example: There are 3 months between the two provided dates; The calendar bar is 935 pixels wide. 935/3(rounded) = 312. Each section of the calendar bar should be approx 312 pixels wide.

Now for the problem. I am using a basic loop to run the math and push the values in to a javascript array. Which works fine. What I would like to do is increment each subsequent value in the array by adding it to the previous value.

Example: 1st value is 312, second value is 614 (312+312), and the final value is 936(312+614) or (312+312+312) however the math has to work.

For some reason, I cannot for the life of me figure out how to make it push the values in this manner.

Here is the code I am currently working with. (note that some of the variables that are being updated may be declared elsewhere in the script)

function divideCalendarBar(){

    var calBarWidth = 935;  
    var calBarDivisions = Math.round(calBarWidth/cb_dateDiff);
    var leftPositions = calBarDivisions;
    var leftPositionsArray = new Array();

    for (var i = 1; i < cb_dateDiff; i++) {     
        leftPositionsArray.push(leftPositions);
    };

    cb_leftPositionsArray = leftPositionsArray;
}

Any thoughts you fine folks might have are much appreciated!

2
  • 3
    leftPositionsArray.push(leftPositions * i);? Commented Feb 21, 2014 at 16:50
  • This worked as well. Ok guys, I'm gonna make sure I drink more coffee before I start posting stupid questions. Thanks for humoring me! Commented Feb 21, 2014 at 16:56

4 Answers 4

4

1st value is 312, second value is 614 (312+312), and the final value is 936(312+614) or (312+312+312)

So you could say your first value is 312 x 1, your second is 312 + 312 = 312 x 2 and your third is 312 + 312 + 312 = 312 x 3.

Since you already have a loop with an index running from 1 to (presumably) 3:

for (var i = 1; i < cb_dateDiff; i++) {     
    leftPositionsArray.push(leftPositions * i);
};

Should work

Sign up to request clarification or add additional context in comments.

3 Comments

I don't think this will work since you never get to i === 3, because you're using the less-than instead of less-than-or-equal
@jwags: Since we have no idea what cb_dataDiffs value actually is, then no.
From the OP, you can see that cb_dateDiff is the number of elements that should end up in the leftPositionArray. Since leftPositions is defined as Math.round(calBarWidth/cb_dateDiff); we can see that the loop should run cb_dateDiff times, and since i starts at 1 it should be <=
2

So I think you should be able to do the following

for (var i = 1; i <= cb_dateDiff; i++) {     
    leftPositionsArray.push(leftPositions * i);
};

Assuming cb_dateDiff is 3 in your example from above

Comments

0

If we add the last value of array to leftPositions then it would be solved your problem

for (var i = 1; i < cb_dateDiff; i++) {    
     if(leftPositionsArray.length > 0){
         leftPositionsArray.push(leftPositions+[leftPositionsArray.length-1]); 
      }else{
           leftPositionsArray.push(leftPositions); 
      } 
};

Comments

0

The answer was originally provided by Matt Burland:

leftPositionsArray.push(leftPositions * i);

Thanks Matt and everyone else.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.