-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

asked Jun 5, 2018 at 0:37
7
  • 1
    These are my key values: Is that an array you have to work with, or an object, or just plain text, or what? Commented Jun 5, 2018 at 0:38
  • Hey thanks for your interest. I need a javascript code. arrays/objects would be good solutions. Commented Jun 5, 2018 at 0:39
  • @alfasin Its similar, but its not exactly what I need Commented Jun 5, 2018 at 0:42
  • @Elron: If it's not, please elaborate how is it different Commented Jun 5, 2018 at 0:43
  • 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 Commented Jun 5, 2018 at 0:49

1 Answer 1

-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
}
answered Jun 5, 2018 at 0:47
Sign up to request clarification or add additional context in comments.

4 Comments

Beautiful!! So simple and neat, I did not know you can store numbers like that. Thank you!
@Elron once you get it working mark it as the accepted answer :)
@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.
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.