# How to improve this "if, else if" code?

**URL:** <https://discourse.nodered.org/t/how-to-improve-this-if-else-if-code/22311>\
**Category:** General\
**Created:** [27 February 2020 04:52 UTC](https://discourse.nodered.org/t/how-to-improve-this-if-else-if-code/22311 "2020-02-27T04:52:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![whatsupskip](https://avatars.discourse-cdn.com/v4/letter/w/5f9b8f/32.png) [@whatsupskip](https://discourse.nodered.org/u/whatsupskip)\
**Post date:** [27 February 2020 04:52 UTC](https://discourse.nodered.org/t/how-to-improve-this-if-else-if-code/22311/1 "2020-02-27T04:52:46Z")

</div>

I tried to use the OR operator || to improve this code, but it didn't work the way I though. Is there anyway to simplify this code. The number are all integers.

> if (HeatPumpTemp\_MB === 19) {HeatPumpTemp\_MB = HeatPumpTemp\_MB + 1;}  
> else if (HeatPumpTemp\_MB === 21) {HeatPumpTemp\_MB = HeatPumpTemp\_MB + 1;}  
> else if (HeatPumpTemp\_MB === 23) {HeatPumpTemp\_MB = HeatPumpTemp\_MB + 1;}  
> else if (HeatPumpTemp\_MB === 25) {HeatPumpTemp\_MB = HeatPumpTemp\_MB + 1;}  
> else {HeatPumpTemp\_MB = HeatPumpTemp\_MB + 2;}

The only option I could think of was:

> if (HeatPumpTemp\_MB === 19 || HeatPumpTemp\_MB === 21 || HeatPumpTemp\_MB === 23 || HeatPumpTemp\_MB === 25) {HeatPumpTemp\_MB = HeatPumpTemp\_MB + 1;}

Which is a slight improvement, but I thought there might have been a better option.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [27 February 2020 05:12 UTC](https://discourse.nodered.org/t/how-to-improve-this-if-else-if-code/22311/2 "2020-02-27T05:12:24Z")

</div>

Not sure what you are trying to achieve; if \< 19 then +2 ?

---

<div class="post-metadata">

**Author:** ![unixneo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/unixneo/32/17441_2.png) [@unixneo](https://discourse.nodered.org/u/unixneo)\
**Post date:** [27 February 2020 06:55 UTC](https://discourse.nodered.org/t/how-to-improve-this-if-else-if-code/22311/3 "2020-02-27T06:55:04Z")

</div>

If you create an array of your HeatPumpTemp\_MB(s) of interest, you can simply check to see if your current HeatPumpTemp\_MB is in your array, if so add one, if not add two:

```auto
let interestingMB = [19,21,23,25];

```

I think you can figure it out from here 🙂

PS: This has very little to do with Node-RED as is just a basic programming 101 question, regardless of the language you are programming in.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [27 February 2020 07:25 UTC](https://discourse.nodered.org/t/how-to-improve-this-if-else-if-code/22311/4 "2020-02-27T07:25:32Z")

</div>

What are the `else` conditions if any?

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [27 February 2020 18:53 UTC](https://discourse.nodered.org/t/how-to-improve-this-if-else-if-code/22311/5 "2020-02-27T18:53:12Z")

</div>

Personally, I like to use the JS `switch` statement for cases like this...  
[https://www.w3schools.com/jsref/jsref\_switch.asp](https://www.w3schools.com/jsref/jsref_switch.asp)

So your example could be rewritten something like this (untested):

```auto
switch(HeatPumpTemp_MB) {
  case 19:
  case 21:
  case 23:
  case 25:
    HeatPumpTemp_MB += 1;
    break;
  default:
    HeatPumpTemp_MB += 2;
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [27 April 2020 18:53 UTC](https://discourse.nodered.org/t/how-to-improve-this-if-else-if-code/22311/6 "2020-04-27T18:53:16Z")

</div>

Closed on 27 April 2020 18:53 UTC

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
