The issue here is relatively simple by taking your code, which is fine, but if you want it to be only 10 steps (as defined in the variable steps
) just one of the following small amendment, which in my opinion has nothing to do with the indexes, that this is well defined:
max=30;
min=-20;
step=10;
arr_text_y=[];
var calc = max - min;
var div = calc / step;
var value = min + div; //não está a ser utilizado.
arr_text_y[0] = min;
for(var i = 1; i < step; i++) //Tirar o igual para ele só passar no ciclo 9 vezes, vai assim ignorar o último valor.
//Para retirar o primeiro em vez do último, poderá utilizar a variável que não está a ser utilizada assim: arr_text_y[0] = value;
{
var val = arr_text_y[i-1] + div;
var fixed = Math.round(val * 100) / 100;
arr_text_y[i] = fixed;
}
console.log(arr_text_y);
Or, for me, the right thing would be:
max=30;
min=-20;
step=10;
arr_text_y=[];
var calc = max - min;
var div = calc / (step-1); //Para que sejam mesmo os valores entre os -20 e os 30 inclusive, em 10 passos.
var value = min + div; //não está a ser utilizado.
arr_text_y[0] = min;
for(var i = 1; i <= step; i++)
{
var val = arr_text_y[i-1] + div;
var fixed = Math.round(val * 100) / 100;
arr_text_y[i] = fixed;
}
console.log(arr_text_y);
This is due to a small mistake made by all of us unconsciously, that we forget that if we want to go from 1 to 10 in 10 steps, we have to take 9 steps, if we consider the minimum, our first number:
calc = max - min = 10 - 1 = 9
.
Thank you for the reply, but you will only put me in the array 6 elements. I wanted you to have the 10 Elements, as the step.
– akm
@akm Only decrease the
step
for 5. =)– stderr
But it also has 11 elements.
– akm
Yes, but the last value, 30, does not appear in the array. Between -20 and 30. These two values must appear in the array, as minimum and maximum.
– akm
@akm If we have as a minimum
-20
, maximum+30
, and the step5
, we have the outworking: -20, -15, -10, -5, 0, 5, 10, 15, 20, 25, 30, you should consider which index starts at 0 in JS, to do this what you want to do, you will have to "sacrifice" an element of array. =)– stderr
@akm Depending on what you want to do, maybe there are other ways.
– stderr