# Returns function value

**URL:** https://discourse.nodered.org/t/returns-function-value/27667
**Category:** General
**Created:** [2 June 2020 14:33 UTC](https://discourse.nodered.org/t/returns-function-value/27667 "2020-06-02T14:33:00Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![bperard](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@bperard](https://discourse.nodered.org/u/bperard)
#### Post date: [2 June 2020 14:33 UTC](https://discourse.nodered.org/t/returns-function-value/27667/1 "2020-06-02T14:33:00Z")

</div>

Hi, everybody,  
I need your help because I’m trying to create a rock, papers, cisors game with the help of the phone sensors and sending values not another but I want to display who won. I can’t return the value because of the function, it never returns any value at all I can’t tell who won. I’ll give you my code ( sorry it’s in French). Hopefully you can help me.

```auto
var msgEleve1 = {payload: msg.payload};
var msgEleve2 = {payload: msg.resultat2};

function comparer(choix1, choix2) {
  if (choix1 === choix2) {
    return 'Egalité !';
  } else if (choix1 === 'pierre') {
    if (choix2 === 'ciseaux') {
      return 'pierre';
    } else if (choix2 === 'feuille') {
      return 'feuille';
    }
  } else if (choix1 === 'feuille') {
    if (choix2 === 'pierre') {
      return 'feuille';
    } else if (choix2 === 'ciseaux') {
      return 'ciseaux';
    }
  } else if (choix1 === 'ciseaux') {
    if (choix2 === 'pierre') {
      return 'pierre';
    } else if (choix2 === 'feuille') {
      return 'ciseaux';
    }
  }
}

  

var resultat=comparer(msgEleve1,msgEleve2);

if (resultat == 'égalité') {
    msg.resultat='Egalité'
    return msg;
  } else if (resultat == msgEleve1) {
      msg.resultat='Eleve 1 a gagné'
    return msg;
  } else {
      msg.resultat='Eleve 2 a gagné'
     return msg;
  }

```

---

<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: [2 June 2020 15:04 UTC](https://discourse.nodered.org/t/returns-function-value/27667/2 "2020-06-02T15:04:10Z")

</div>

You need to be sure what is going into the function is what you expect.

For example, your function `comparer` has no final `else` and will likely be falling through.

Also in your code, msgEleve1 & msgEleve2 are objects with a `.payload` property - cant compare objects like this ` if (choix1 === choix2) {` so instead, take the string & compare that instead.

try this - see if it highlights your issue...

```auto
var msgEleve1 = msg.payload || "";
var msgEleve2 = msg.resultat2 || "";

const validChoices = ["pierre","feuille","ciseaux"];

if(!validChoices.includes(msgEleve1)) {
    node.warn(`msgEleve1 '${msgEleve1}' is not valid`)
    return null;
}
if(!validChoices.includes(msgEleve1)) {
    node.warn(`msgEleve2 '${msgEleve2}' is not valid`)
    return null;
}

function comparer(choix1, choix2) {
    if (choix1 === choix2) {
        return 'Egalité !';
    } else if (choix1 === 'pierre') {
        if (choix2 === 'ciseaux') {
            return 'pierre';
        } else if (choix2 === 'feuille') {
            return 'feuille';
        }
    } else if (choix1 === 'feuille') {
        if (choix2 === 'pierre') {
            return 'feuille';
        } else if (choix2 === 'ciseaux') {
            return 'ciseaux';
        }
    } else if (choix1 === 'ciseaux') {
        if (choix2 === 'pierre') {
            return 'pierre';
        } else if (choix2 === 'feuille') {
            return 'ciseaux';
        }
    }
}

var resultat = comparer(msgEleve1, msgEleve2);

if (!resultat) {
    return null; //halt flow
}

if (resultat == 'égalité') {
    msg.resultat = 'Egalité'
} else if (resultat == msgEleve1) {
    msg.resultat = 'Eleve 1 a gagné'
} else {
    msg.resultat = 'Eleve 2 a gagné'
}

return msg; //return msg to next node

```

---

<div class="post-metadata">

### Author: ![bperard](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@bperard](https://discourse.nodered.org/u/bperard)
#### Post date: [2 June 2020 15:48 UTC](https://discourse.nodered.org/t/returns-function-value/27667/3 "2020-06-02T15:48:35Z")

</div>

Thank you for your help. But I’m a beginner in this area so I don’t see how to compare from the string at all. Can you help me more?

---

<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: [2 June 2020 16:58 UTC](https://discourse.nodered.org/t/returns-function-value/27667/4 "2020-06-02T16:58:27Z")

</div>

Doesn't the example posted by @Steve-Mcl show you how to do it?

---

<div class="post-metadata">

### Author: ![dxdc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dxdc/32/23114_2.png) [@dxdc](https://discourse.nodered.org/u/dxdc)
#### Post date: [2 June 2020 17:30 UTC](https://discourse.nodered.org/t/returns-function-value/27667/5 "2020-06-02T17:30:08Z")

</div>

There are a few issues here @bperard:

1. `égalité` is an impossible returned value:  
i.e., `if (resultat == 'égalité') {`

`comparer` returns `Egalité !` not `égalité`

1. `comparer` function is expecting two strings as input, not two objects.

```auto
var msgEleve1 = {payload: msg.payload};
var msgEleve2 = {payload: msg.resultat2};

```

1. There is no check for valid inputs. Unexpected outcomes will result if something other than `feuille`, `ciseaux`, or `pierre` are used as inputs.

Points 2 & 3 were addressed by @Steve-Mcl. The first point was not.

It needs to be:  
`if (resultat == 'Egalité !') {`

After that, the script he provided should work as you expect.

---

<div class="post-metadata">

### Author: ![bperard](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@bperard](https://discourse.nodered.org/u/bperard)
#### Post date: [2 June 2020 17:44 UTC](https://discourse.nodered.org/t/returns-function-value/27667/6 "2020-06-02T17:44:24Z")

</div>

Thank you for your help. I finally got what I wanted. Good evening to you.

---

<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: [1 August 2020 17:54 UTC](https://discourse.nodered.org/t/returns-function-value/27667/7 "2020-08-01T17:54:37Z")

</div>

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