-2

I have this random score (a number) between 0-72.

Each score number has a key number that I have set. (look below)

I need to retrieve the key number by using a function.

Example:

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

I figure you need to use arrays somehow but I can't figure out how.

These are my key values:

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

I would really appreciate your help, I can't figure this out and I also don't know what to search for really.

By the way, your code can be in ES6, I don't mind :) Thanks in advance

Elron
  • 1,235
  • 1
  • 13
  • 26
  • 1
    `These are my key values:` Is that an array you have to work with, or an object, or just plain text, or what? – CertainPerformance Jun 05 '18 at 00:38
  • Hey thanks for your interest. I need a javascript code. arrays/objects would be good solutions. – Elron Jun 05 '18 at 00:39
  • @alfasin Its similar, but its not exactly what I need – Elron Jun 05 '18 at 00:42
  • @Elron: If it's not, please elaborate how is it different – Isaac Jun 05 '18 at 00:43
  • @CertainPerformance I mean, these 2 lists can be made into arrays or obejcts, it doesn't really matter – Elron Jun 05 '18 at 00:43
  • @Isaac Hey Isaac, as you can see in my key values, if the number is 24 or below I need it to always return -100. Same for the high numbers, anything higher than 72+ should return 100. – Elron Jun 05 '18 at 00:45
  • 2
    It sounds like your only actual issue here is taking the time to write out the values in Javascript format and put that into your code – CertainPerformance Jun 05 '18 at 00:49

1 Answers1

-1

Your first thing would be how are you storing the keys and scores?

The way i would do it would be when generating the keys and scores.

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

then all you would have to do to get the data is

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

scores would be an object and look like this

{
   72:100
   71:99
   70:98
   69:97
   68:96
   67:95
   66:94
   65:92
   64:90
}
Chris
  • 82
  • 12
  • Beautiful!! So simple and neat, I did not know you can store numbers like that. Thank you! – Elron Jun 05 '18 at 00:50
  • 1
    @Elron once you get it working mark it as the accepted answer :) – Chris Jun 05 '18 at 00:56
  • 1
    @Elron: It's exactly shown as answer in the link I've shared you and you said it's different. Now you say you didn't know can store numbers like that. Lol. – Isaac Jun 05 '18 at 03:49
  • Hey Issac, its not exactly the same - the missing thing that I did not understand is that its possible to set numbers as keys and give them value of numbers. This is what I needed and it has not been shown as examples in the duplicate answer. However, what matters is that the problem is solved now. Thank you – Elron Jun 08 '18 at 22:23