新手问题 求更精简的写法

i5ting · 2014年11月20日 · 最后由 winnie 回复于 2014年11月20日 · 1789 次阅读

把 0 转成 A,把 1 转成 B,直到 Z

目前我写的

js 代码

function i_to_char(z) {
    var i = z;
    if(z >= 0 && z < 26) {
        i = i + 65;
    }else{
        return "undefined z in i_to_char(z)"
    }

    return String.fromCharCode(i);
}

大家有更精简的写法?

function i_to_char(z) {
    return (z >= 0 && z < 26) ? String.fromCharCode(z + 65) : "undefined z in i_to_char(z)"
}
Number.prototype.to_char = function() {
  return (this >= 0 && this < 26) ? String.fromCharCode(this + 65) : "number >= 0 and < 25"
};
Number.prototype.to_char = function() {
  return String.fromCharCode(this + 65)
};

0..to_char() 1..to_char() 2..to_char()

#1 楼 @miclle 第一个和我的做法一样,还能更精简么?

4 楼 已删除

('A'...'Z').to_a.at(x)

需要 登录 后方可回复, 如果你还没有账号请 注册新账号