Home Web Front-end JS Tutorial Nodejs modularization: nodejs calculation permutations and combinations (code)

Nodejs modularization: nodejs calculation permutations and combinations (code)

Aug 15, 2018 pm 04:23 PM

The content of this article is about nodejs modularization: nodejs calculation permutation and combination (code), which has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Using nodejs to calculate permutations and combinations is an example:

Permutations and combinations require factorial, so create new main.js and jiecheng.js files and pailiezuhe.jsFile (because of learning modularization, so create two files)

Copy the following code into jiecheng.js, comment in detail

var abc = 100;

// 计算阶乘的方法
function jiecheng(n){
    var con = 1;
    for(n;n>0;n--){
        con = con*n;
    } 
    return con;
}

// module.exports 表示本js文件所导出的内容,默认是一个空对象
// 通过给modul.exports赋值可以设置本js文件所导出的内容
module.exports = jiecheng;
Copy after login

The last one above is the export The current factorial method

So when calling pailiezuhe.js, the factorial method is first imported

pailiezuhe.js code:

console.log(123);

// 使用require可以在一个js文件中导入另一个js文件
// 参数表示要导入的js文件,内容是要导入的js文件的路径(可以是相对路径也可以是绝对路径)
// 返回值是导入的js文件中所导出的内容

//  ./表示本js文件所在的目录
var jc = require("./jiecheng.js");


// 使用require导入一个js文件仅仅是将这个js文件导出的内容导入,然后赋值给一个变量,并不会导入这个js文件中的其他内容,也就是说这两个js文件的作用域是隔离的
// console.log(abc);

function pailie(n,m){
    return jc(n)/jc(n-m);
}

function zuhe(n,m){
    return jc(n)/(jc(m)*jc(n-m));
}
// 使用module.exports只能导出一个内容,如果需要导出多个内容,可以把这些内容封装成一个对象,然后导出这个对象
// module.exports = {
//     pailie:pailie,
//     zuhe:zuhe
// };
// 或者是给exports添加属性也是一样
module.exports.pailie = pailie;
module.exports.zuhe = zuhe;
Copy after login

main.js code

// 当代码量很大时,可以将代码分散在多个js文件中,每个js文件单独实现一个小功能,这些js文件共同组成一个完整的大功能

// 在浏览器环境中,可以通过多个script标签导入多个js文件.但是这种合并代码的方法有一下缺点:
// 1,多个js文件的导入必须按照依赖关系先后导入
// 2,使用script标签导入的多个文件本质上是拼接成了一个js文件,所以这些js文件运行时都处于同一个全局作用域,那么这些js文件中不能使用同名的全局变量.


// -------------------------------------------------------------------------------

// 在nodejs中也可以实现将不同的功能写入不同的js文件,在某个js文件中需要什么功能就导入哪个功能的js文件
// 这叫做nodejs的模块化

// 当 第一次 导入某个js文件时,这个js文件会执行并获得导出内容,如pailiezuhe.js里console.log()会执行
var plzh = require("./pailiezuhe.js");

// 某个js文件运行得到导出内容之后,导出的内容会被缓存起来,下次再导入这个js文件时,就会直接获得上次的导出结果,不再运行js文件,例如下面这两行如果不注释也不会执行pailiezuhe.js的console
// var q = require("./pailiezuhe.js");
// var w = require("./pailiezuhe.js");

var n = 10;
var m = 4;
console.log(plzh.pailie(n,m));
console.log(plzh.zuhe(n,m));
Copy after login

Related Recommendation:

Easily create nodejs server (3): Code modularization_node.js

nodejs require module (file module/core Module) and path introduction_Basic knowledge

The above is the detailed content of Nodejs modularization: nodejs calculation permutations and combinations (code). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Can PowerPoint run JavaScript? Can PowerPoint run JavaScript? Apr 01, 2025 pm 05:17 PM

JavaScript can be run in PowerPoint, and can be implemented by calling external JavaScript files or embedding HTML files through VBA. 1. To use VBA to call JavaScript files, you need to enable macros and have VBA programming knowledge. 2. Embed HTML files containing JavaScript, which are simple and easy to use but are subject to security restrictions. Advantages include extended functions and flexibility, while disadvantages involve security, compatibility and complexity. In practice, attention should be paid to security, compatibility, performance and user experience.

See all articles