0

I have JS code which I am trying to convert to C#. For some reason my C# method is returning 10 less than the return value of the JS function. I have tried changing multiple things and also checking the meaning of the && operator in JS but can't seem to figure out what I am doing wrong.

The correct return value is 97.

JavaScript Function & Usage:

function rir(t, e, c, n) {
    return t > e 
        && t <= c 
        && (t += n % (c - e)) > c 
        && (t = t - c + e),
       t
}
rir('a'.charCodeAt(0), 47, 57, 'b'.charCodeAt(0));
/* returns 97 */

C# Method & Usage:

public int Rir(int t, int e, int c, int n)
{
    if (t > e && t <= c)
        t += (n % (c - e));
    if (t > c)
        t = ((t - c) + e);
    return t;
}
Rir((int)'a', 47, 57, (int)'b');
/* returns 87 */
5
  • 2
    Those single-letter variable names and assignments in the middle of expressions definitely don't help readability. Oh dear Commented Apr 26, 2018 at 3:18
  • I don't know very much about JavaScript, this JavaScript function was pre written and also most likely was minified (guessing by the terrible naming convention). I am just trying to copy it over to C# verbatim. Commented Apr 26, 2018 at 3:20
  • @kingdaro I was thinking the same thing. Whoever wrote that needs to be punched in the face. Commented Apr 26, 2018 at 3:30
  • @Darkrum Like OP says, it's probably minified, but I still wouldn't be surprised if it were handwritten. Seen some shit, haha Commented Apr 26, 2018 at 3:31
  • Note that 'a' is a JavaScript string but a C# char Commented Apr 26, 2018 at 3:32

1 Answer 1

2

(t = t - c + e) requires the first three condition to be true.

public int Rir(int t, int e, int c, int n)
{
    if (t > e && t <= c)
    {
        t += (n % (c - e));
        if (t > c)
            t = ((t - c) + e);
    }
    return t;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.