最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript return and ternary operator with var, possible? - Stack Overflow

programmeradmin1浏览0评论

I'm sorry if this question is a duplicate, but I really don't know how to search for it. This question may sound "odd" for an expert JavaScript programmer, but I'm not.

I'm basically trying to do a "one line return", without wasting another line of code. I know that it's not good, and the following it's not code for production:

var _ = require('underscore');

module.exports = function (digits) {
    if (!/^\d+$/.test(digits)) return undefined;

    var prep = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9];

    var sum = _.reduce(digits.toString(), function (mem, dgt, idx) {
        return mem + (idx % 2 == 0 ? parseInt(dgt) : prep[dgt]);
    }, 0);

    return (var mod = sum % 10 == 0) ? 0 : 10 - mod; // Error
};

The last line throws an error because the var keyword. I remember doing sometimes the same in PHP.

EDIT: I don't think so "hard" to read the question before answer... I'm asking if it's possible, I'm not saying it's right, good looking, or whatever.

(By the way this is the luhn check calculation)

I'm sorry if this question is a duplicate, but I really don't know how to search for it. This question may sound "odd" for an expert JavaScript programmer, but I'm not.

I'm basically trying to do a "one line return", without wasting another line of code. I know that it's not good, and the following it's not code for production:

var _ = require('underscore');

module.exports = function (digits) {
    if (!/^\d+$/.test(digits)) return undefined;

    var prep = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9];

    var sum = _.reduce(digits.toString(), function (mem, dgt, idx) {
        return mem + (idx % 2 == 0 ? parseInt(dgt) : prep[dgt]);
    }, 0);

    return (var mod = sum % 10 == 0) ? 0 : 10 - mod; // Error
};

The last line throws an error because the var keyword. I remember doing sometimes the same in PHP.

EDIT: I don't think so "hard" to read the question before answer... I'm asking if it's possible, I'm not saying it's right, good looking, or whatever.

(By the way this is the luhn check calculation)

Share Improve this question edited May 24, 2013 at 19:09 gremo asked May 24, 2013 at 19:01 gremogremo 48.5k80 gold badges271 silver badges447 bronze badges 2
  • 2 I'm just wondering, are you downvoting all those legitimate and still correct answers? – Marcel Gwerder Commented May 24, 2013 at 19:24
  • 1 btw: you'll have to put mod = sum % 10 in parentheses otherwise it won't work because mod would be the result of the == operator and thus a boolean. – basilikum Commented May 24, 2013 at 19:36
Add a ment  | 

6 Answers 6

Reset to default 10

VariableDeclaration are not expressions. Just declare it before.

module.exports = function (digits) {
    var mod;

    // ...

    return (mod = sum % 10 ...
}

I think you're trying too hard here. Just move var mod before the return statement. You're NOT going to be struck dead by the software gods for having one more line of code here. Clarity over conciseness.

JavaScript is not PHP

var mod = sum % 10;
return (mod === 0) ? 0 : 10 - mod;

I think the only way for you to do this is:

var mod = sum % 10;
return (mod == 0) ? 0 : 10 - mod;

Who cares about one extra line of code? Why does that matter?

I see you don't like any of the answers so far. One way to avoid declaring the variable first is this, which you probably won't like either:

return (sum % 10 == 0) ? 0 : 10 - (sum % 10);

This doesn't require an extra line, but it does require an extra mod.


Another option, which might make the code extemely confusing, is to add a dummy argument to the function:

module.exports = function (digits, mod) {
    /* code */
    return (mod = sum % 10) == 0 ? 0 : 10 - mod; // Error
};

Since you don't use sum for any other purpose, you could move the % operator up, doing:

var _ = require('underscore');

module.exports = function (digits) {
    if (!/^\d+$/.test(digits)) return undefined;

    var prep = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9];

    var sum = _.reduce(digits.toString(), function (mem, dgt, idx) {
        return mem + (idx % 2 == 0 ? parseInt(dgt) : prep[dgt]);
    }, 0) % 10;

    return (sum == 0) ? 0 : 10 - sum; // Error
};
发布评论

评论列表(0)

  1. 暂无评论