【问题标题】:Codewars Kata - 4th Kyu - Codewars style ranking system - "+1 problem"Codewars Kata - 4th Kyu - Codewars 风格排名系统 - “+1 问题”
【发布时间】:2021-05-04 18:32:49
【问题描述】:

本以为这个kata会很酷,休息后很容易回到js。 我错了大声笑。 因此,kata 的 URL 包含所有逻辑和数学信息,我将把必要的放在下面。

网址: https://www.codewars.com/kata/51fda2d95d6efda45e00004e/train/javascript

代码:

class User {
    constructor() {
        this.rank = -8;
        this.progress = 0;
    this.rankTable=[-8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8];
    this.rankIndicator=0;
    }
  
    incProgress(rankOfActivity) {
    if (rankOfActivity == 0 || rankOfActivity > 8 || rankOfActivity < -8) throw new error("Rank input out of range");
        if (rankOfActivity <= this.rank - 2) return;
    let diff = this.rankTable.indexOf(rankOfActivity)-this.rankTable.indexOf(this.rank);
    if (diff==0) this.progress+=3;
    else if(diff==-1) this.progress+=1;
    else if(this.rank!=8){
      this.progress+= 10*diff*diff;
      while(this.progress>=100 && this.rank<8){
        this.progress-=100;
        this.rankIndicator++;
        this.rank=this.rankTable[this.rankIndicator];
      }
    }
    if (this.rank==8) this.progress=0;
  }
}

完成比用户排名低一级的活动将获得 1 分

看完之后,我的观点是: 如果活动排名与用户排名的差值为-1,例如:

用户的排名为 -2(rankTable 数组中的索引 6)

活动排名为 -3(rankTable 数组中的索引 5)

差值为 5-6 = -1

所以它应该加起来 1 点进度,而且看起来它没有这样做,我无法弄清楚为什么它不加起来。

这里有一堆错误表明它发生在任何等级。

应用 -1 等级后,进度预计为 21,但实际上是 20

应用等级 3 后进度预计为 61,但实际上是 60

应用 8 级后,进度预计为 51,但实际上是 50

【问题讨论】:

  • @ulou Nah,kata 说 min 是 -8 max 是 8。所以你可以从 -8 进步到 8 不包括 0

标签: javascript arrays


【解决方案1】:
    //...
    //if (rankOfActivity <= (this.rank - 2)) return;
    //bug
    let diff = (this.rankTable.indexOf(rankOfActivity)) - (this.rankTable.indexOf(this.rank));
    
    if (diff <=-2)return;//
    //fixed
    //...

就个人而言,我不喜欢在这里使用数组和indexOf。使用 [0 到 15] 的排名计数器会好一些。

【讨论】:

    猜你喜欢
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多