Home  >  Article  >  Web Front-end  >  Calculate the factors of a positive integer using JavaScript

Calculate the factors of a positive integer using JavaScript

藏色散人
藏色散人Original
2021-08-13 17:37:413856browse

In "Detailed explanation of how to multiply and divide two numbers through javascript", we introduce how to multiply and divide two numbers through javascript. Interested friends can Learn and learn~

The topic of this article is "How to write a JavaScript function to calculate the factors of a positive integer"?

So what is the factor? This belongs to elementary school knowledge. Everyone should know that a factor means that the quotient of an integer a divided by an integer b (b≠0) is exactly an integer without a remainder. We say that b is a factor of a.

After understanding what a factor is, we can easily calculate the factor of a positive integer through js code.

The complete implementation code is as follows:




    
    

We still use console.log() to view the output information, as follows:

Calculate the factors of a positive integer using JavaScript

Obviously the factors of 15, 16, and 17 are [1,3,5,15], [1,2,4,8,16], [1,17] respectively.

In the above code, the methods that everyone needs to know are:

sqrt()Method: can return the square root of a number;

→Note: The Math.pow() method can calculate any root of a number.

floor()Method: A number can be rounded down;

→Note: The floor() method performs downward rounding Rounding calculation, it returns the nearest integer that is less than or equal to the function parameter.

push()Method: One or more elements can be added to the end of the array and the new length is returned;

→Note: This method will change the length of the array. To add one or more elements to the beginning of the array, use the unshift() method.

sort()Method: used to sort the elements of the array.

→Note: The array is sorted on the original array and no copy is generated.

Finally, I would like to recommend "JavaScript Basics Tutorial" ~ Welcome everyone to learn ~

The above is the detailed content of Calculate the factors of a positive integer using JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn