How does the front end deal with the written algorithm? (programming with node)

Source: Internet
Author: User
Tags stdin

Writing algorithm problems with Nodejs

We use the algorithm at the front end of the place is not many, but for the school recruit written test, have to solve the algorithm problem to practice!

Finally determined to conquer the algorithm problem. It is not possible to find JS to build the input and output stream as the C language itself. Can only go back to learn C language? Actually not, node can also help us to complete! and the written test support with Nodejs, in fact, is to use JS programming, just use the node of some input and output stream method.

Let's look at the simplest use of templates: (reprint)

var readline = require(‘readline‘);rl = readline.createInterface({    input: process.stdin,    output: process.stdout});rl.on(‘line‘, function(data) {    // 获取输入    var inputs = data.trim().split(‘ ‘);    // 处理    var result = deal(inputs);    // 输出结果    console.log(result);});function deal(inputs) {    var result = ‘‘;    // dosomething    return result;}

This simple set of templates will solve the general algorithm problem! Finally put up a front end algorithm problem Example:
Title: Enter n (n<=10000) numbers to find the maximum and minimum values in these n numbers. The absolute value of each number is not greater than 1000000.

var readline = require(‘readline‘);var rl = readline.createInterface({    input:process.stdin,    output:process.stdout,});var num = 0; rl.on(‘line‘,function(input){    if(num==0){        num = input.trim();    }    else{        var iptArr = input.split(‘ ‘);        if(iptArr.length==num){            var maxNum = Math.max.apply(null,iptArr);            var minNum = Math.min.apply(null,iptArr);            var result = maxNum + ‘ ‘ + minNum;            console.log(result);            return result;        }    }})//在终端输入node max_min        //文件名5                    //输入N个数的数量12 18 5 20 10        //N个数20 5                //返回结果

For more ways to use node in algorithmic questions, see: https://www.cnblogs.com/floor/p/6667059.html
Front-end interview common algorithm questions see this: http://web.jobbole.com/88471/

How does the front end deal with the written algorithm? (programming with node)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.