Thursday, October 16, 2025
HomeLanguagesJavascriptHow to create slider to map a range of values in JavaScript...

How to create slider to map a range of values in JavaScript ?

The task is to map a range of values to another range of values like map (0-100) to (100-10000000) in JavaScript. There are two approaches that are discussed below.

Approach 1: Use the logarithmic scale to map the range. In this example, first, the log value of the range is calculated and then the scale is calculated using the difference of log values of the target scale to the difference of the start scale. Then use the exponentiation to map the range for a particular number.

Example: This example implements the above approach.

html




<body style="text-align:center;">
    <h1>GeeksForGeeks</h1>
    <h3>
        Create slider to map a range of values
    </h3>
  
    <p id="GFG_UP"></p>
  
    <button onclick="myGFG()">
        Click Here
    </button>
  
    <p id="GFG_DOWN"></p>
    <script>
        var up = document.getElementById("GFG_UP");
        var val = 50;
        up.innerHTML = "Click on the button to map"
            + " the value(0-100) to a range from "
            + "100 to 10, 000, 000<br> Val - " + val;
          
        var down = document.getElementById("GFG_DOWN");
          
        function Slider(pos) {
            var minM = 0;
            var maxM = 100;
            var minV = Math.log(100);
            var maxV = Math.log(10000000);
            var scal = (maxV - minV) / (maxM - minM);
            return Math.exp(minV + scal * (pos - minM));
        }
        function myGFG() {
            down.innerHTML = "The value for "
                + val + " is " + Slider(val);
        }
    </script>
</body>


Output:

 Create slider to map a range of values

Approach 2: This example also maps the value from one range to another but uses a different formula.

Example: This example implements the above approach.

html




<body style="text-align:center;">
    <h1>GeeksForGeeks</h1>
    <h3>
        Create slider to map a range of values in JavaScript
    </h3>
  
    <p id="GFG_UP"></p>
  
    <button onclick="myGFG()">
        Click Here
    </button>
  
    <p id="GFG_DOWN"></p>
  
    <script>
        var up = document.getElementById("GFG_UP");
        var val = 50;
        up.innerHTML = "Click on the button to map"
            + " the value(0-100) to a range from "
            + "100 to 10, 000, 000<br> Val - " + val;
          
        var down = document.getElementById("GFG_DOWN");
        function Slider(pos) {
            return Math.floor(-900 + 1000 *
                    Math.exp(pos / 10.857255959));
        }
        function myGFG() {
            down.innerHTML = "The value for "
            + val + " is " + Slider(val);
        }
    </script>
</body>


Output:

 Create slider to map a range of values

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS