8000 Added comparison functions (GT/LT) to dice: !roll 1d20 + x > y by NicholasClaaszen · Pull Request #21 · ryanguill/watney · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Added comparison functions (GT/LT) to dice: !roll 1d20 + x > y #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Added comparison functions (GT/LT) to dice: !roll 1d20 + x > y #21

wants to merge 5 commits into from

Conversation

NicholasClaaszen
Copy link
Contributor

critique welcome, as always.

I think checkTarget() can be streamlined, but I'm not sure how

} else {
text = ' Failure! ' + result + ' is higher than ' + target;
}
} else {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could the quantifier be equal or not equal to? or what about GTE/LTE?

- Changed the checkTarget function to use different, cooler syntax.
- Included E, GTE and LTE checks.
@NicholasClaaszen
Copy link
Contributor Author

Updated pull request with suggestions.

};
if (quantifiers[quantifier.toLowerCase()](parseFloat(result), parseFloat(target))) text = ' `Success!`';
return text;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8000

this is getting nitpicky, there really isnt anything wrong with what you have (except for maybe the parsefloat()) but here is how I would write this function:

function checkTarget(result, quantifier, target) {

    //do your argument manipulation, defaulting, etc at the top of the function
    //making your use of them more clear
    quantifier = quantifier.toLowerCase();
    //either use lodash's parseFloat or use parseFload(result, 10)
    result = _.parseFloat(result);
    target = _.parseFloat(target);

    let quantifiers = {
        //personal style preferences
        '>': (a, b) => a > b,
        '&lt;': (a, b) => a < b,
        '&gt;=': (a, b) => a >= b,
        '&lt;=': (a, b) => a <= b,
        '=': (a, b) => a === b
    };

    //this line is much easier to read with the manipulation stuff already done
    if (quantifiers[quantifier](result, target)){
        //I personally only use bracket-less if statements when doing an early bail-out return,
        //like when the function should just not run - errors or something like that
        return ' `Success!`';
    }
    //no need to store the result, just return it where necessary
    return ' `Failure!`'
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more info on parseInt() http://stackoverflow.com/questions/16880327/why-am-i-getting-weird-result-using-parseint-in-node-js-different-result-from

you can also use Number() in most cases instead of parseInt

@atuttle
Copy link
Collaborator
atuttle commented Oct 6, 2017

If/when this lands it will require updates to the !roll docs added in #52

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0