javaScript -- js 数字转段位(王者荣耀段位)

王者荣耀段位js实现。

每一个等级的提升都是一个数字的增加,而至于段位不过是划分而已。找到范围就是了。

王者荣耀的段位可以分3个层级

铜、银、铁.. 是最外层的等级
然后就是'I', 'II', 'III', 'IV', 'V'
再是 最后的等级

代码

/**
 * 段位等级
 * num 等级数字
 */
const getGrade = (num) => {
  var luomashuzi = ['I', 'II', 'III', 'IV', 'V']
  var data = [
    { x: 3, y: 3, name: '青铜' },
    { x: 4, y: 4, name: '白银' },
    { x: 4, y: 4, name: '黄金' },
    { x: 5, y: 5, name: '铂金' },
    { x: 5, y: 5, name: '钻石' },
  ]
  data = data.map(e => ({ ...e, total: e.x * e.y }))
  for (var d = 0; d < data.length; d++) {
    data[d].range = [...data].splice(0, d).map(e => e.total).reduce((a, b) => a + b, 0)
  }
  // console.log(data)
  for (var d = 0; d < data.length; d++) {
    var item = data[d]
    var x = item.x
    var y = item.y
    // 段位判断
    if (num > item.range && num <= item.total + item.range) {
      for (let i = 1; i <= y; i++) {
        // 获取第二段位
        if (num - item.range <= y * i) {
          // 获取第四段位(星级)并返回结果
          return {
            y: item.y,
            name: item.name + ' ' + luomashuzi[x - i],
            star: (x - (x * i - (num - item.range)))
          }
        }
      }
    }
  }
  return {
    name: '学霸',
    star: num - data.map(e => e.total).reduce((a, b) => a + b, 0)
  }
}

(上面是js代码和小程序没啥关系,不过写的示例是在小程序上面,所以归类小程序)

使用

this.setData({
      grade_list: new Array(100).fill(0).map((_, i) => i + 1)
      // grade_list: [9, 50, 100]
      .map(util.getGrade)
        .map(e => {
          if(e.name == '学霸'){
            return {
              ...e,
              isTopGrade: true,
            }
          }
          return { ...e, star: this.setStar(e.y, e.star), isTopGrade: false,}
        })
    })
<view wx:for="{{grade_list}}">
    <text>{{item.name}}</text>
    <view class="img-box" wx:if="{{!item.isTopGrade}}">
      <image wx:for="{{item.star}}" :key="key" class="{{item == 0 ? 'gray' : 'full'}}" src="{{item == 0 ? '/image/gray-star.png' : '/image/star.png'}}"></image>
    </view>
    <text wx:else>{{item.star}}</text>
  </view>

打印看看

        var v = ``
        for(let i = 1; i = 100; i++){
            v += i + ': ' + JSON.stringify(getGrade(i)) + '\n'
        }
        console.log(v)

打印结果

1: {"y":3,"name":"青铜 III","star":1}
2: {"y":3,"name":"青铜 III","star":2}
3: {"y":3,"name":"青铜 III","star":3}
4: {"y":3,"name":"青铜 II","star":1}
5: {"y":3,"name":"青铜 II","star":2}
6: {"y":3,"name":"青铜 II","star":3}
7: {"y":3,"name":"青铜 I","star":1}
8: {"y":3,"name":"青铜 I","star":2}
9: {"y":3,"name":"青铜 I","star":3}
10: {"y":4,"name":"白银 IV","star":1}
11: {"y":4,"name":"白银 IV","star":2}
12: {"y":4,"name":"白银 IV","star":3}
13: {"y":4,"name":"白银 IV","star":4}
14: {"y":4,"name":"白银 III","star":1}
15: {"y":4,"name":"白银 III","star":2}
16: {"y":4,"name":"白银 III","star":3}
17: {"y":4,"name":"白银 III","star":4}
18: {"y":4,"name":"白银 II","star":1}
19: {"y":4,"name":"白银 II","star":2}
20: {"y":4,"name":"白银 II","star":3}
21: {"y":4,"name":"白银 II","star":4}
22: {"y":4,"name":"白银 I","star":1}
23: {"y":4,"name":"白银 I","star":2}
24: {"y":4,"name":"白银 I","star":3}
25: {"y":4,"name":"白银 I","star":4}
26: {"y":4,"name":"黄金 IV","star":1}
27: {"y":4,"name":"黄金 IV","star":2}
28: {"y":4,"name":"黄金 IV","star":3}
29: {"y":4,"name":"黄金 IV","star":4}
30: {"y":4,"name":"黄金 III","star":1}
31: {"y":4,"name":"黄金 III","star":2}
32: {"y":4,"name":"黄金 III","star":3}
33: {"y":4,"name":"黄金 III","star":4}
34: {"y":4,"name":"黄金 II","star":1}
35: {"y":4,"name":"黄金 II","star":2}
36: {"y":4,"name":"黄金 II","star":3}
37: {"y":4,"name":"黄金 II","star":4}
38: {"y":4,"name":"黄金 I","star":1}
39: {"y":4,"name":"黄金 I","star":2}
40: {"y":4,"name":"黄金 I","star":3}
41: {"y":4,"name":"黄金 I","star":4}
42: {"y":5,"name":"铂金 V","star":1}
43: {"y":5,"name":"铂金 V","star":2}
44: {"y":5,"name":"铂金 V","star":3}
45: {"y":5,"name":"铂金 V","star":4}
46: {"y":5,"name":"铂金 V","star":5}
47: {"y":5,"name":"铂金 IV","star":1}
48: {"y":5,"name":"铂金 IV","star":2}
49: {"y":5,"name":"铂金 IV","star":3}
50: {"y":5,"name":"铂金 IV","star":4}
51: {"y":5,"name":"铂金 IV","star":5}
52: {"y":5,"name":"铂金 III","star":1}
53: {"y":5,"name":"铂金 III","star":2}
54: {"y":5,"name":"铂金 III","star":3}
55: {"y":5,"name":"铂金 III","star":4}
56: {"y":5,"name":"铂金 III","star":5}
57: {"y":5,"name":"铂金 II","star":1}
58: {"y":5,"name":"铂金 II","star":2}
59: {"y":5,"name":"铂金 II","star":3}
60: {"y":5,"name":"铂金 II","star":4}
61: {"y":5,"name":"铂金 II","star":5}
62: {"y":5,"name":"铂金 I","star":1}
63: {"y":5,"name":"铂金 I","star":2}
64: {"y":5,"name":"铂金 I","star":3}
65: {"y":5,"name":"铂金 I","star":4}
66: {"y":5,"name":"铂金 I","star":5}
67: {"y":5,"name":"钻石 V","star":1}
68: {"y":5,"name":"钻石 V","star":2}
69: {"y":5,"name":"钻石 V","star":3}
70: {"y":5,"name":"钻石 V","star":4}
71: {"y":5,"name":"钻石 V","star":5}
72: {"y":5,"name":"钻石 IV","star":1}
73: {"y":5,"name":"钻石 IV","star":2}
74: {"y":5,"name":"钻石 IV","star":3}
75: {"y":5,"name":"钻石 IV","star":4}
76: {"y":5,"name":"钻石 IV","star":5}
77: {"y":5,"name":"钻石 III","star":1}
78: {"y":5,"name":"钻石 III","star":2}
79: {"y":5,"name":"钻石 III","star":3}
80: {"y":5,"name":"钻石 III","star":4}
81: {"y":5,"name":"钻石 III","star":5}
82: {"y":5,"name":"钻石 II","star":1}
83: {"y":5,"name":"钻石 II","star":2}
84: {"y":5,"name":"钻石 II","star":3}
85: {"y":5,"name":"钻石 II","star":4}
86: {"y":5,"name":"钻石 II","star":5}
87: {"y":5,"name":"钻石 I","star":1}
88: {"y":5,"name":"钻石 I","star":2}
89: {"y":5,"name":"钻石 I","star":3}
90: {"y":5,"name":"钻石 I","star":4}
91: {"y":5,"name":"钻石 I","star":5}
92: {"name":"学霸","star":1}
93: {"name":"学霸","star":2}
94: {"name":"学霸","star":3}
95: {"name":"学霸","star":4}
96: {"name":"学霸","star":5}
97: {"name":"学霸","star":6}
98: {"name":"学霸","star":7}
99: {"name":"学霸","star":8}
100: {"name":"学霸","star":9}

--完--

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,743评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,296评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,285评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,485评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,581评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,821评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,960评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,719评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,186评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,516评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,650评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,329评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,936评论 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,757评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,991评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,370评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,527评论 2 349

推荐阅读更多精彩内容