-2

This is the code that works, but it isn't scalable to varying array lengths.

How can I simplify this?

if(section_time_cumulative[0]<= time && 
    section_time_cumulative[1]>=time) 
{ 
    linears[0] 
} 
else if(section_time_cumulative[1]<= time && 
    section_time_cumulative[2]>=time) 
{ 
    linears[1] 
} 
else if(section_time_cumulative[2]<= time && 
    section_time_cumulative[3]>=time) 
{ 
    linears[2] 
} 
else if(section_time_cumulative[3]<= time && 
    section_time_cumulative[4]>=time) 
{ 
    linears[3] 
} 
else if(section_time_cumulative[4]<= time && 
    section_time_cumulative[5]>=time) 
{ 
    linears[4] 
} 
else if(section_time_cumulative[5]<= time && 
    section_time_cumulative[6]>=time) 
{ 
    linears[5] 
} 
else if(section_time_cumulative[6]<= time && 
    section_time_cumulative[7]>=time) 
{ 
    linears[6] 
} 
else if(section_time_cumulative[7]<= time && 
    section_time_cumulative[8]>=time) 
{ 
    linears[7] 
} 
else 
{ 
    0 
}

Expected result:

enter image description here

I've tried this way, but it doesn't produce the desired result; it's resulting in only the 'last' iteration of values being defined as I expect.

for(arrIndex = 0 ; arrIndex< section_bars.length-1; arrIndex++) 
{
    if(section_time_cumulative[arrIndex]<= time && 
        section_time_cumulative[arrIndex+1]>=time) 
    { 
        linears[arrIndex] 
    } 
    else
    {
        0
    }

}

Note only the last values output (far right end of graph):

only values at far right of graph

2
  • arrIndex< section_bars.length-1 should be arrIndex < section_bars.length-2 or arrIndex <= section_bars.length-1 since you want arrIndex to be from 0 to 7, not to to 8. Commented 3 hours ago
  • @jonsharpe - thanks for catching that! Commented 2 hours ago

2 Answers 2

0

You need to not overrun and as important: You need to return the value you found when you find the value.

var result = 0;

for (var i = 0, n = section_time_cumulative.length - 1; i < n; i++) {
  if (section_time_cumulative[i] <= time &&
      section_time_cumulative[i + 1] >= time) {
    result = linears[i];
    break;   // stops scanning after the correct match
  }
}

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

2 Comments

not sure I follow... when I add "return" I get an error ("Error: Syntax Error: Illegal return statement")
See update.....
0

You could use a function end return early by assuming

  • section_time_cumulative array has incrementing values.
function getValue() {
    for (let index = 0; index < section_bars.length - 1; index++) {
        if (
            section_time_cumulative[index] <= time &&
            section_time_cumulative[index + 1] >= time
       ) return linears[index];
    }
    return 0; 
}

1 Comment

This works.. though, I'm not sure I understand why?

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.