How to make a sum in a loop for or while?

Asked

Viewed 342 times

2

I need x to be added to 7 after the loop, but I’m not getting it, the code works perfectly if I do x++, is there any way to make that sum with 7?

        var x;
        for (x = dtdia + 2; x <= 31; x+ 7){
            console.log(x);
          };

Explaining the context, I want to save an event weekly in the database, so I thought I’d create a loop that identifies today and the day of the week that this event will take place and add up to 7 until the 31st. The complete code to better understand:

    var dt = new Date();
    var weekday = dt.getDay();
    var dtdia = dt.getDate();
    var diasem = document.getElementById('DiaSem').value;

    var soma = weekday - diasem;

switch (soma) {
        case -1:
            // 1dia
            var x;
            for (x = dtdia + 1; x <= 31; x+ 7){
                console.log(x);
              };
          break;
          case  -2:
            // 2dia
              var x;
            for (x = dtdia + 2; x <= 31; x+ 7){
                console.log(x);
              };
          break;
};
  • But since doing with x++ the code would work you could think about putting x=x+6; inside the loop

3 answers

3


Using x++ works because the operator ++ changes the value of x. So if you want to add seven, just do x + 7 does not work because it does not modify the value of x. The right thing to do x += 7 or x = x + 7:

for (x = dtdia + 1; x <= 31; x += 7)
    etc...

But in fact this will not work properly for what you need. For example, what if the month does not have 31 days and by chance on for the value of x for 31? The same goes for February, if x is greater than 28 (or 29 in leap years), you will print 30 or 31 as valid days?

In that case you’d better go updating the Date, and only print if you are still in the current month:

var dt = new Date(); // data atual
var mesAtual = dt.getMonth(); // mês atual

var diasem = 3; // quarta-feira (coloquei algum valor qualquer só de exemplo)

// pega a próxima quarta feira 
dt.setDate(dt.getDate() + (diasem + (7 - dt.getDay())) % 7);

// enquanto não mudar o mês
while (dt.getMonth() == mesAtual) {
    console.log(dt.getDate());
    dt.setDate(dt.getDate() + 7); // soma 7 dias
}

This works because if the value of the day is greater than the amount of days of the month, setDate sets the date automatically for the following month.

In the example above I put 3 for the day of the week (which is equivalent to Wednesday), just to illustrate. But in your case, I suggest you transform the value of the input in number:

var diasem = parseInt(document.getElementById('DiaSem').value);

For it avoids some problems that may happen if you don’t make this conversion.

  • 1

    +1 because it was the only answer that took into account that working with dates will not be a simple sum, because in this context it is necessary to consider that the days of the week may end in another month.

  • 1

    Thanks, I had managed to solve the question problem later, but you ended up helping me with other things!!

1

An alternative is to use the While. Like you suggested I could use too. Initializes the variable x according to the logic you thought to validate.

var x = inicializaComOValorSalvo;
while(x<=31){
  console.log(x);
  x+=7;
};

0

The way to insert more values in a sum would be like this. the value X +(sum) e = the value 7.

x+=7

var x;
for (x = 4 + 1; x <= 31; x+=7){
  console.log(x);
};

Browser other questions tagged

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