新手问题 求更精简的写法

i5ting · November 20, 2014 · Last by winnie replied at November 20, 2014 · 1790 hits

把 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 Floor has deleted

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

You need to Sign in before reply, if you don't have an account, please Sign up first.