2

I am very new to jscript programming so any help you can provide would be greatly appreciated.

I have created a two dimensional array of 5 rows and 4 columns. The LOOKUP_TAX_POSITION is a function call that retrieves user input data. The column labeled base is not input by the user but needs to be calculated based on the other information input by the user. After loading the table, I am updating the value in the "column" labeled base for rows 1-4 using the previous "rows" data. The base value for row 0 will remain 0.

I am using jscript and have searched a million ways to determine how, or if it is possible to reference the +1 index in a For Loop with no success. Thanks for any guidance you can provide. Here is my code

var RateTableArray = [  
  {min: ('0'),                           max: LOOKUP_TAX_POSITION ( 'FDMX1' ), base: ('0'), rate:LOOKUP_TAX_POSITION ( 'RATE1')},
  {min: LOOKUP_TAX_POSITION ( 'FDMN2' ), max: LOOKUP_TAX_POSITION ( 'FDMX2' ), base: ('0'), rate:LOOKUP_TAX_POSITION ( 'RATE2' )},
  {min: LOOKUP_TAX_POSITION ( 'FDMN3' ), max: LOOKUP_TAX_POSITION ( 'FDMX3' ), base: ('0'), rate:LOOKUP_TAX_POSITION ( 'RATE3' )},
  {min: LOOKUP_TAX_POSITION ( 'FDMN4' ), max: LOOKUP_TAX_POSITION ( 'FDMX4' ), base: ('0'), rate:LOOKUP_TAX_POSITION ( 'RATE4' )},
  {min: LOOKUP_TAX_POSITION ( 'FDMN5' ), max: ('0'),                           base: ('0'), rate:LOOKUP_TAX_POSITION ( 'RATE5')},
];

for (i=0; i <= 4 ;i++)  {             
  Base = (RateTableArray[i].max - RateTableArray[i].min) * RateTableArray[i].rate + RateTableArray[i].base ; 
  RateTableArray[i+1].base = (Base) ;   //this is where I am stuck
} 
1
  • Initial look at the code looks correct, are you getting a error of some kind? Commented Nov 21, 2014 at 21:50

3 Answers 3

1

It is not clear to me with what you mean by //this is where I am stuck.

The only thing I can think of is that for the last entry you are setting base for a non-existant array index and perhaps you get a TypeError?

This can be solved by changing your for loop to

for ( i=0; i < RateTableArray.length-1 ; i++){ 

Furthermore, the codereviewer in me cringes at that lengthy piece of code and would prefer to see

var rate, base;
for ( i=0; i < RateTableArray.length-1 ; i++){ 
    rate = RateTableArray[i];           
    base = (rate.max - rate .min) * rate.rate + rate.base; 
    RateTableArray[i+1].base = base;
} 
Sign up to request clarification or add additional context in comments.

3 Comments

I love information on how to clean up code. thanks for the suggestion.
The code is working beautifully. Thanks for the advise.
Also, feel free to visit codereview.stackexchange.com for cleaning code up.
0

your problem is with the following piece of code

for (i=0; i <= 4 ;i++)

your looping until i <= 4 which means when i is 4, your RateTableArray[i+1] will be accessing the index at 5 which is non existent. Your loop needs to be

for (i=0; i < 4 ;i++)

you need to remove the = sign.

Comments

0

I prefer a functional style when operating on elements of an array. It takes a little getting used to but is easier and more readable once you get the hang of it.

RateTableArray.map(function (rate, i, array) {
  if (array[i+1]) {
    array[i+1].base = (rate.max - rate.min) * rate.rate + rate.base;
  }
});

Another potential problem I see is that your values are strings for some of the min, max, base values. Depending on the operation, you may not get the expected results.

1 Comment

Thanks so much for the advise. I work in a team that is completely non-technical so where possible I try to stick to as close to plain english as possible. I do appreciate the approach you proposed though. Also, the tip about the strings was the last piece of the puzzle. Thanks for taking the time to review and provide feedback.

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.