How to create a function that defines a value between two numbers

I have this range of numbers:

0 -------> 25 -------> 80 ------> 150 small medium large 

I want to get a number from 0 to 150 and display whether it is small, medium or large. 30 and 45 are average because they are between 25 and 80 and 5 are small because they are below 25.

I want to create a function that matches this object:

 var sizeMap = { small : 25, medium : 80, large : 150 } 

(assuming 0 is the smallest number).

The function should look like this:

 function returnSize(number) { for (item in sizeMap) ??????? return size } 

how to write this function so that it can be flexible for adding new categories (for example: "extra large": 250). Should I present an object as an array?

+6
source share
6 answers

To have a deterministic result , I would prefer to have an array sorted by relevant categories:

 var categories = [ {label: 'small', upTo: 25}, {label: 'medium', upTo: 80}, {label: 'large', upTo: 150} ]; 

(you can even create this array from the original sizeMap )

And then you can do:

 function getSizeCategory(number, categories) { for (var i = 0; i < categories.length; ++i) { if (number < categories[i].upTo) { return categories[i].label; } } throw new Error("not in range!"); } 
+3
source
 function returnSize(number, sizeMap) { for (var key in sizeMap) { if (number < sizeMap[key]) return key; } return key; } 
+2
source

just return the highest value that satisfies the less condition:

 function returnSize (number) { var item; for (item in sizeMap) { if (number < sizeMap[item]) { return item; } } return item; // this will return largest size if it gets this far } 
+1
source

You must use arra options. By order of DESC.

You check your number for your first number (the highest) if you need to comply with the upper limit.

And when you are below or equal to the upper limit of a category, you know which category it belongs to.

0
source

Here you have a solution:

 RangeCheck = function(range) { this.init(range); }; RangeCheck.prototype = { constructor: RangeCheck, /** * The range map. */ range: null, init: function (range) { this.range = range || {}; }, putRange: function(name, max) { this.range[name] = max; }, check: function(number) { for (var key in this.range) { if (this.range.hasOwnProperty(key)) { if (number <= this.range[key]) { return key; } } } return key; } }; 

Use it as follows:

 var rangeCheck = new RangeCheck({ small : 25, medium : 80, large : 150 }); var message = "15: " + rangeCheck.check(15) + "\n" + "25: " + rangeCheck.check(25) + "\n" + "85: " + rangeCheck.check(85) + "\n" + "850: " + rangeCheck.check(850) + "\n"; alert(message); rangeCheck.putRange("veryLarge", 300); var message = "15: " + rangeCheck.check(15) + "\n" + "25: " + rangeCheck.check(25) + "\n" + "85: " + rangeCheck.check(85) + "\n" + "850: " + rangeCheck.check(850) + "\n"; alert(message); 

And jsFiddle: http://jsfiddle.net/xKjt7/

0
source

Just for fun. One liner using underscore :

 get_size = function (n) { return _.find(_.map({'small':100, 'big':200}, function (x,i) { return [x < n, i]; }), function (x) { return x[0] == false; })[1];}; 
0
source

Source: https://habr.com/ru/post/923883/


All Articles