Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the JavaScript ratio function

Shulou Source: shulou.com Published: 2022-06-01 10:31:08 09月30日 Update

This article mainly introduces the relevant knowledge of how to use the JavaScript ratio function, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this JavaScript ratio function. Let's take a look at it.

JavaScript ratio function

The purpose of the comparison function is to define another sort order.

The comparison function should return a negative, zero, or positive value, depending on the parameter:

Function (a, b) {return Amurb}

When the sort () function compares two values, it sends the values to the comparison function and sorts them based on the values returned (negative, zero, or positive).

Example:

When comparing 40 and 100, the sort () method calls the comparison function function (40100).

This function calculates 40-100 and returns-60 (negative).

The sort function will sort 40 to a value lower than 100.

You can use the following code snippet to test numeric and alphabetical ordering:

Sort in numerical order

Var points = [40,100,1,5,25,10]

Document.getElementById ("demo"). InnerHTML = points

Function myFunction1 () {

Points.sort ()

Document.getElementById ("demo"). InnerHTML = points

}

Function myFunction2 () {

Points.sort (function (a, b) {return a-b})

Document.getElementById ("demo"). InnerHTML = points

}

This is the end of the article on "how to use the JavaScript ratio function". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use the JavaScript ratio function". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Tags: Function sort ratio knowledge order content letter article two code value parameter depending on example simple operation numerical value number article method easy to understand Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Xiaomi vpn OPPO Reno MySQL