score:1
you can use an array of objects with properties set to from "poor"
to "great"
corresponding to the range of numbers set as value of the property, array.prototype.filter()
var range = [{
poor: [0, 20]
}, {
average: [21, 50]
}, {
good: [51, 80]
}, {
great: [81, 100]
}];
var number = 45;
var res = range.filter(function(el) {
var key = el[object.keys(el)];
return number > key[0] && number < key[1]
});
console.log(object.keys(res[0])[0])
score:0
var ranges = [
{
name: 'poor',
range: [0, 20]
},
{
name: 'average',
range: [21, 50]
},
{
name: 'good',
range: [51, 80]
},
{
name: 'great',
range: [81, 100]
},
]
var number = 45;
var range = _.find(ranges, function (r) {
return _.inrange(number, r.range[0], r.range[1] + 1);
}).name;
score:1
the d3 way using scales:
var scale = d3.scalequantize()
.domain([0,100])
.range(["very bad","bad","average","good","very good"]);
console.log(scale(34));
console.log(scale(55));
console.log(scale(91));
<script src="https://d3js.org/d3.v4.min.js"></script>
the nice thing of d3 is that the scale automatically divides the domain based on the number of values of the range. for instance, the snippet above has 5 values ("very bad","bad","average","good","very good"), and so 34 is "bad". in the snippet below, using only 3 values, 34 is "average":
var scale = d3.scalequantize()
.domain([0,100])
.range(["bad","average","good"]);
console.log(scale(34));
console.log(scale(55));
console.log(scale(91));
<script src="https://d3js.org/d3.v4.min.js"></script>
score:3
this should work...
var range = [[0, 20], [21, 50], [51, 80], [81, 100]]
var number = 45
var bucket = range.filter(function(a) {
if (number >= a[0] && number <= a[1]) return a
})
console.log(bucket[0])
Source: stackoverflow.com
Related Query
- Determine the bucket to which a number belongs to
- Dynamically resize the d3 tree layout based on number of childnodes
- maximum number of svg elements for the browser in a map
- How to force nvd3 to display the equal number of ticks as that of the values plotted on the graph-nvd3
- Counting the number of continuous blue bars till the hover bar
- Count the number of rows of a CSV file, with d3.js
- Change which notation d3.format uses for the 'g' type
- How do I specify the number of axis tick marks in d3?
- Is it possible to have a scrolling div that adjusts to the number of bars in a dc.js rowchart
- How to make the NVD3 discreteBarChart labels on the X axis to adapt to the width or to the number of labels?
- Limit number of Y axis ticks by keeping a top tick above the bar in d3
- Create Sankey/ Alluvial diagram in which the links combine at the node
- How to append javascript file to the body from javascript file which is in the same directory. Java server faces 2 JSF2
- How do you move number labels further away from the x-axis in d3.js?
- Why does converting a negative string into a number ( +"-2.333" ) return NaN, only when the string came from d3.format function?
- Which one is faster, selecting the element directly or selecting its parent first?
- How can I determine the overall size of a force directed graph
- Dynamically set the axisBottom number of ticks to the highest integer in an array in D3v4
- How do I get the index number from the array in d3?
- Customise the number format in Apache superset
- Calculating the number of bars in a bar chart in dc.js
- D3: How to add an extra column to the table which is not in csv columns
- Changing color range in a d3 reusable donut chart depending on the number of data points
- y axis vertical the range is auto ticked as 5 even if i changed to different number
- Reducing the number of anonymous functions needed in calls to D3's `attr` and the like
- how can I see the current element that represents a bar to which the hovering is applied
- How to set the width of td as float number
- Guarantee the number of ticks in D3
- D3 v4 (Tree Diagram) - Dynamically calculate the height of the SVG given dynamic number of nodes
- How do I make my text keep the variable number and not the reference in javascript D3?
More Query from same tag
- d3v4 scatterplot - fixing the domain adding animations
- How do I modify column headers after importing the CSV?
- Is traversing all elements in the (MVC) Model necessary every time it is updated?
- use d3 treemap to go upto child on going on clicking parent to child nodes
- How do I get d3 to treat missing values as null instead of zero?
- How to pass a number from app.js to index.html? I can pass a static text, but need to pass a calculated number or number from an array
- Transitioning basis line to linear line shortens/hide part of the line
- in D3.js is there any way bind the scroll event in both the x and y directions to panning the svg?
- Add updating tooltips to a streamgraph
- Plot a Network with specified coordinates in D3.js
- How to cumulate d3.rollup data by year
- Token Error in Makefile for D3js
- D3 JSON file with source and index as strings rather than indices
- d3 multi line chart is overflowing
- make text a link in svg
- D3.js transitioning multiple arc paths
- How to apply gradient mask on dots
- Adding a new path when the updated data has additional sections
- Understanding a multi-line d3 chart example
- Keeping track of the current number of values selected when the brush is moved, using dc.js/crossfilter.js
- how can I fix these margins (black bars) in my CSS stylesheet
- Using $.getScript to load d3.js
- D3 cannot read property offsetHeight of null
- how to add array of text to multiple divs with d3
- d3.js Multi level pie chart with per-slice offset start angles
- Breaking line in Bootstrap tooltip inside D3 svg
- D3.geo.bounds does not return bounding box?
- d3 pie chart not loading
- d3.js - Smooth transition on arc draw for pie chart
- React native with d3 and webView