1

I want to reset a count value to 0 after reaching a certain limit. And return the value which is (originalCount - limit).

I have tried if and for loops but cannot achieve what I want.

//payload is an array of bytes//
if (payload[0] == 33) {
    result.PayloadTtype = 'Vehicle Count';
    if (payload[2] > 80) {
        payload[2] -= 80; //Sensor Reboot due to exceeding limit
        result.Count = payload[2];
    }else {
        result.Count = payload[2];
    }
}

I can return correct values up to 159 but once it exceeds 160 on wards it prints (80,81....). I want it to start again from 0 even for multiples of 80n (n = 1,2,3...).

tobsob
  • 602
  • 9
  • 22
Hemanth
  • 13
  • 4
  • what's wrong with `payload[2] = 0;` ? – I wrestled a bear once. Aug 26 '19 at 12:36
  • @Iwrestledabearonce. I think he wants a kind of circularity, and `90` should evaluate to `10`, not `0` – FZs Aug 26 '19 at 12:43
  • @Iwrestledabearonce. I can make payload[2] = 0; but that will only print 0. I want to reset it to 0 and start counting from there again. And can anyone tell me how to use markdown while adding a comment? – Hemanth Aug 26 '19 at 12:57
  • @Hemanth In comments `\`asd\`` creates code formatting (`asd`), `*asd*` or `_asd_` creates italic (*asd*), `[title](url)` creates link ([title](https://stackoverflow.com/questions/57657996/count-restart-from-0-after-exceeding-n-times-the-limit/)) and finally `**asd**` and `__asd__` creates bold (__asd__). You can test [here](https://meta.stackexchange.com/questions/3122/formatting-sandbox) – FZs Aug 26 '19 at 14:09

1 Answers1

1

Take the modulus of payload[2] by 80:

if (payload[0] == 33) {
    result.PayloadTtype = 'Vehicle Count';
    if (payload[2] > 80) {
        payload[2] %= 80; //Sensor Reboot due to exceeding limit
        result.Count = payload[2];
    }else {
        result.Count = payload[2];
    }
}

You can also move the unconditional statement (the assignment of result.Count) out of the if statement, in which case you don't even need the if (modulus will leave values smaller than 80 intact):

if (payload[0] == 33) {
    result.PayloadTtype = 'Vehicle Count';
    payload[2] %= 80;
    result.Count = payload[2];
}

And if you really wanted to make things compact, you can even do this (though it harms readability in my opinion):

if (payload[0] == 33) {
    result.PayloadTtype = 'Vehicle Count';
    result.Count = payload[2] %= 80;
}
FZs
  • 16,581
  • 13
  • 41
  • 50
  • Thanks for the solution. Both the ways work for me. – Hemanth Aug 26 '19 at 13:04
  • sure did not recognize the tick mark was for that. I have one more query how can I retain the leading zeros while converting from any to binary. I looked up at several solutions not what I want. I'm decoding date from the payload and I cannot pad zeros manually. – Hemanth Aug 27 '19 at 21:00
  • To store leading zeros, you have to convert the number to string. If you can use ES2017, then use [`String#padStart()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart), like `number.toString().padStart(2/*expected length*/, 0)`. Also see [this SO question](https://stackoverflow.com/questions/1267283/how-can-i-pad-a-value-with-leading-zeros)! – FZs Aug 28 '19 at 07:15