Step in a for cycle

Asked

Viewed 78 times

4

I want to put inside a array, values between -20 and 30, the part of a size that is 10. My problem is that I can put the values, but the size of the array is 11 values.

max=30;
min=-20;
step=10;
arr_text_y=[];

var calc = max - min;
var div = calc / step;
var value =  min + div;
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);

3 answers

3


In Javascript the index of a array starts from the 0, nay 1, so the result of your code is right.

Alternatively you can do:

max  = 30;
min  =-20;
step = 5;
arr_text_y = [];

for (var i = min; i <= max; i += step){
    if (arr_text_y.length == 10) // Se tiver 10 elementos, interrompe.
       break;
    arr_text_y.push(i);
}
alert(arr_text_y);

  • 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 Only decrease the step for 5. =)

  • But it also has 11 elements.

  • 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 If we have as a minimum -20, maximum +30, and the step 5, 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. =)

  • @akm Depending on what you want to do, maybe there are other ways.

Show 1 more comment

1

You’re making a big mistake in your job.

1º- 30 -(-20) = 50. se sua ideia era que desse 10 voce deve colocar min=20, ou max+min(sendo 30+(-20)=10).

2º- Se voce quer que passe 10x no for seria (i=1; i<=10; i++)

3º- var fixed = Math.round(val * 100) / 100; Sempre será fixed=val.Creio que seu intuito seria Math.round(val / 100) * 100; - para arredondar.

0

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

.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.