javascript 用键数组替换数字数组

javascript replace array of numbers by array of keys

我有这个随机分数(一个数字)在 0-72 之间。

每个分数编号都有一个我设置的关键编号。 (往下看)

我需要使用函数检索密钥编号。

示例:

function scoreToKey(score) {
     // the mystery code goes here
     return PERCENTAGE;
}

scoreToKey(100);  // needs to return 100
scoreToKey(72);   // needs to return 100
scoreToKey(50);   // needs to return 39
scoreToKey(44);   // needs to return 4
scoreToKey(29);   // needs to return -92
scoreToKey(12);   // needs to return -100
scoreToKey(0);    // needs to return -100

我认为您需要以某种方式使用数组,但我不知道如何使用。

这些是我的关键值:

score | key
72  100
71  99
70  98
69  97
68  96
67  95
66  94
65  92
64  90
63  87
62  84
61  81
60  78
59  75
58  72
57  69
56  65
55  61
54  57
53  53
52  49
51  44
50  39
49  34
48  28
47  22
46  16
45  10
44  4
43  ‎-2
42  ‎-8
41  ‎-16
40  ‎-24
39  ‎-32
38  ‎-40
37  48
36  56
35  ‎-64
34  ‎-70
33  ‎-76
32  ‎-82
31  ‎-68
30  ‎-90
29  ‎-92
28  ‎-94
27  ‎-96
26  ‎-98
25  ‎-99
1-24    ‎-100

非常感谢您的帮助,我无法弄清楚,我也不知道该搜索什么。

顺便说一句,你的代码可以是 ES6,我不介意:) 提前致谢

您的第一件事是如何存储密钥和分数?

我的做法是生成密钥和分数。

function addScore(score, key){
  scores[score] = key;
}

那么你需要做的就是获取数据

function getScore(score){
   return scores[scores];
}

分数将是一个对象,看起来像这样

{
   72:100
   71:99
   70:98
   69:97
   68:96
   67:95
   66:94
   65:92
   64:90
}