# Function node help

**URL:** <https://discourse.nodered.org/t/function-node-help/52122>\
**Category:** General\
**Created:** [12 October 2021 08:55 UTC](https://discourse.nodered.org/t/function-node-help/52122 "2021-10-12T08:55:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![davenrea](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/davenrea/32/32842_2.png) [@davenrea](https://discourse.nodered.org/u/davenrea)\
**Post date:** [12 October 2021 08:55 UTC](https://discourse.nodered.org/t/function-node-help/52122/1 "2021-10-12T08:55:29Z")

</div>

Hi

I must be missing something simple but i am trying to return from the below number to zero decimal places, nothing i try seems to work, any suggestion appreciated

```auto
const numbers = [bedroomtemp, bedroomventtemp, loungeventtemp, loungetemp];
let max = Math.max.apply(null, numbers);
let min = Math.min.apply(null, numbers);

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 October 2021 08:59 UTC](https://discourse.nodered.org/t/function-node-help/52122/2 "2021-10-12T08:59:39Z")

</div>

What output are you expecting?

The main and max of 4 numbers as integer?  
The values and name of which variable was min/max?  
The result as an array? Object?

A combination of `Array.map` & `Math.round` or `Math.ceil` will probably be what you want.

---

<div class="post-metadata">

**Author:** ![davenrea](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/davenrea/32/32842_2.png) [@davenrea](https://discourse.nodered.org/u/davenrea)\
**Post date:** [12 October 2021 09:01 UTC](https://discourse.nodered.org/t/function-node-help/52122/3 "2021-10-12T09:01:51Z")

</div>

I am expecting two variables, one "min" and one "max". I tried Math.round but could't get it to work, quite convinced it was a case of incorrect formatting when i typed it in

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 October 2021 09:14 UTC](https://discourse.nodered.org/t/function-node-help/52122/4 "2021-10-12T09:14:14Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/0/b02b81608422c599489765aa0129966970e7a0f4.png)

```auto
//dummy data - update as required
const bedroomtemp = 21.2, bedroomventtemp=20.9, loungeventtemp=23.123, loungetemp=18.321
const numbers = [bedroomtemp, bedroomventtemp, loungeventtemp, loungetemp]
msg.payload = {
  max: Math.round(Math.max.apply(null, numbers)),
  min: Math.round(Math.min.apply(null, numbers))
}
return msg

```

---

<div class="post-metadata">

**Author:** ![davenrea](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/davenrea/32/32842_2.png) [@davenrea](https://discourse.nodered.org/u/davenrea)\
**Post date:** [12 October 2021 09:49 UTC](https://discourse.nodered.org/t/function-node-help/52122/5 "2021-10-12T09:49:51Z")

</div>

thanks, copied your code and changed to make it how i need it but can't get it through to the next node. If i send msg.control = {min:10, max:80} it works fine and the debug gives me the below image

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/c/6c311c129c286c29a329ea6957e7d398f704920a.png)

whne i try the round statemement it give me this

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/5/f526e049efa15005fc7a4f3e755cbdd76cbe07e9.png)

```auto
const numbers = [bedroomtemp, bedroomventtemp, loungeventtemp, loungetemp]
msg.control = {
minmax: {min:(Math.round(Math.min.apply(null, numbers))), max:(Math.round(Math.max.apply(null, numbers)))}

}
return msg

//msg.control = {min:10, max:80}

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 October 2021 09:53 UTC](https://discourse.nodered.org/t/function-node-help/52122/6 "2021-10-12T09:53:15Z")

</div>

> [@davenrea](#):
>
> If i send msg.control = {min:10, max:80} it works fine and the debug gives me the below image

if thats what you need, then dont add the `minmax` property!

```javascript
const numbers = [bedroomtemp, bedroomventtemp, loungeventtemp, loungetemp]
msg.control = { 
  min: (Math.round(Math.min.apply(null, numbers))), 
  max:(Math.round(Math.max.apply(null, numbers)))
}
return msg

```

or if it makes more sense to you...

```javascript
const numbers = [bedroomtemp, bedroomventtemp, loungeventtemp, loungetemp]
msg.control = { };
msg.control.min = Math.round(Math.min.apply(null, numbers)); 
msg.control.max = Math.round(Math.max.apply(null, numbers));
return msg

```

---

<div class="post-metadata">

**Author:** ![davenrea](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/davenrea/32/32842_2.png) [@davenrea](https://discourse.nodered.org/u/davenrea)\
**Post date:** [12 October 2021 09:55 UTC](https://discourse.nodered.org/t/function-node-help/52122/7 "2021-10-12T09:55:34Z")

</div>

> [@Steve-Mcl](#):
>
> ```auto
> const numbers = [bedroomtemp, bedroomventtemp, loungeventtemp, loungetemp]
> msg.control = { };
> msg.control.min = Math.round(Math.min.apply(null, numbers)); 
> msg.control.max = Math.round(Math.max.apply(null, numbers));
> return msg
> 
> ```

Thanks, works perfectly and the second option made it clear in my head, don't do much of this and sometimes can't see the wood for the trees

---

<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:** [26 October 2021 09:56 UTC](https://discourse.nodered.org/t/function-node-help/52122/8 "2021-10-26T09:56:29Z")

</div>

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