0

If the input is a number (e.g) 78.

Now,
0 to 60, belongs to RANK C,
61 to 90, belongs to RANK B,
91 to 100, belongs to RANK A.

Then, if we can't directly to compare 78 and area. like if (61<num && num<90) or switch case or any conditional,it's not allowed!
how can we find the rank number 78 belongs to ?

Here is my solution

I now have a solution to the problem, I use the Array.

let recordMap = [60, 90, 100]
recordeMap.push(78)
recordMap.sort((a,b)=>a-b)

now, the array is [60, 78, 90, 100],
then I just need to know 78's index, recordMap.findIndex(e=>e==78) I can to known 78 is RNAK B.

Is there a better solution?

Kirito K
  • 29
  • 1
  • 1
  • 10
  • How about Ternary operator ? Allowed ? – ValeriF21 Apr 19 '21 at 06:38
  • Does this answer your question? [Get the closest number out of an array](https://stackoverflow.com/questions/8584902/get-the-closest-number-out-of-an-array) – Oliver Apr 19 '21 at 06:40
  • Ternary operator just like if else , you can't direct judgment 78 and 61~90 – Kirito K Apr 19 '21 at 06:48
  • [Get the closest number out of an array](https://stackoverflow.com/questions/8584902/get-the-closest-number-out-of-an-array) My answar is to use array.it''s different question. – Kirito K Apr 19 '21 at 06:50

3 Answers3

2

You could take conditional opererators.

const
    getRank = value => value <= 60
        ? 'C'
        : value <= 90
            ? 'B'
            : 'A'

console.log(getRank(78)); // B

An approach with an array by taking the index and adding an offset for getting a character.

const
    getRank = value => String.fromCharCode(
        [90, 60, 0].findIndex(v => value > v) + 65
    );

console.log(getRank(91)); // A
console.log(getRank(90)); // B
console.log(getRank(28)); // C
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

You can use Regex to check if number is in some range.

(?:[0-9]|[1-5][0-9]|60) --> Rank 'C'
(?:6[1-9]|[78][0-9]|90) --> Rank 'B'
(?:9[1-9]|100) --> Rank 'A'

For example:

console.log("Test if 78 is in range 61-90");
console.log(/^(?:6[1-9]|[78][0-9]|90)$/.test(78));
ValeriF21
  • 454
  • 1
  • 3
  • 14
  • even if this works, it is not useful if you need to maintain the values. the other draback is, you need still a condition to check the result of the test. – Nina Scholz Apr 19 '21 at 07:24
  • True, but i think i any case you will have at least one condition. Another optimization of that one could be putting those Regex as object value and the keys would be A, B, C And than you could iterate through the object. (Just a suggestion) @NinaScholz – ValeriF21 Apr 19 '21 at 09:37
-1

I now have a solution to the problem, I use the Array.

let recordMap = [60, 90, 100]
recordeMap.push(78)
recordMap.sort((a,b)=>a-b)

now, the array is [60, 78, 90, 100],
then I just need to know 78's index, recordMap.findIndex(e=>e==78) I can to known 78 is RNAK B.

Kirito K
  • 29
  • 1
  • 1
  • 10
  • 3
    Either this is an answer, then there shouldn't be a question. Or _"is there a more optimized solution"_ is the actual question then this "answer" should be in the question. – Andreas Apr 19 '21 at 06:41
  • 1
    If you want to get another rank, you'd have to remove the number . Why not `recordMap.findIndex(a => (a - number) > 0)`? You don't have to push the number and sort. – adiga Apr 19 '21 at 06:51