Invert scale function

Javascriptd3.js

Javascript Problem Overview


This is interesting to me. Look at the following D3 code:

var scale = d3.scale.linear()
	.domain([100, 500])
	.range([10, 350]);
	
scale(100);  //Returns 10
scale(300);  //Returns 180
scale(500);  //Returns 350

Is there a function that inverse of scale? For example,

inverseScale(10);   //Returns 100
inverseScale(180);  //Returns 300
inverseScale(350);  //Returns 500

Javascript Solutions


Solution 1 - Javascript

Yes, there is, and it's aptly named invert.

console.log(scale.invert(10));   //Returns 100
console.log(scale.invert(180));  //Returns 300
console.log(scale.invert(350));  //Returns 500

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionngungoView Question on Stackoverflow
Solution 1 - JavascriptOrionMeltView Answer on Stackoverflow