Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptD3.js | d3.variance() function

D3.js | d3.variance() function

The d3.variance() function in D3.js is used to return the variance of the given array’s elements. If the array is having less than two elements then it returns undefined.

Syntax:

d3.variance(Array)

Parameters: This function accepts a parameters Array which is an array of elements whose variance are to be calculated. Here elements should be integers not string otherwise it returns undefined.

Return Value: It returns the variance of the given array’s element.

Below programs illustrate the d3.variance() function in D3.js.
Example 1:




<html>
  
<head>
    <title>
      Getting variance of
      the elements of given array
  </title>
</head>
  
<body>
    <script src='https://d3js.org/d3.v4.min.js'>
  </script>
  
    <script>
        // initialising the array of elements
        var Array1 = [10, 20, 30, 40, 50, 60];
        var Array2 = [1, 2];
        var Array3 = [0, 1.5, 6.8];
        var Array4 = [.8, .08, .008];
  
        // Calling to d3.variance() function
        A = d3.variance(Array1);
        B = d3.variance(Array2);
        C = d3.variance(Array3);
        D = d3.variance(Array4);
  
        // Getting variance of the given array's element
        document.write(A + "<br>");
        document.write(B + "<br>");
        document.write(C + "<br>");
        document.write(D + "<br>");
    </script>
</body>
  
</html>


Output:

350
0.5
12.763333333333332
0.19180800000000003

Example 2:




<html>
  
<head>
    <title>
      Getting variance of the
      elements of given array
  </title>
</head>
  
<body>
    <script src='https://d3js.org/d3.v4.min.js'>
  </script>
  
    <script>
        // initialising the array of elements
        var Array1 = [1];
        var Array2 = ["a", "b", "c"];
        var Array3 = [1, 2, "B", "C"];
        var Array4 = ["Geek", "Geeks", 2, 3, "neveropen"];
  
        // Calling to d3.variance() function
        A = d3.variance(Array1);
        B = d3.variance(Array2);
        C = d3.variance(Array3);
        D = d3.variance(Array4);
  
        // Getting variance of the given array's element
        document.write(A + "<br>");
        document.write(B + "<br>");
        document.write(C + "<br>");
        document.write(D + "<br>");
    </script>
</body>
  
</html>


Output:

undefined
undefined
0.5
0.5

Note: In the above output, if the parameter is having less than one elements or strings then it returns undefined and if the parameter is having string including some integers value then the variance of the integer’s value is returned.

Ref: https://devdocs.io/d3~4/d3-array#variance

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

Recent Comments