~~  BABY STEPS  ~~

Write a program that accepts one or more numbers as command-line

arguments and prints the sum of those numbers to the console (stdout).

----------------------------------------------------------------------
HINTS:

You can access command-line arguments via the global `process` object.
The `process` object has an `argv` property which is an array
containing the complete command-line. i.e. `process.argv`.

To get started, write a program that simply contains:

console.log(process.argv)

Run it with `node myprogram.js` and some numbers as arguments. e.g:

$ node myprogram.js 1 2 3

In which case the output would be an array looking something like:

[ 'node', '/path/to/your/program.js', '1', '2', '3' ]

You'll need to think about how to loop through the number arguments so
you can output just their sum. The first element of the process.argv
array is always 'node', and the second element is always the path to
your program.js file, so you need to start at the 3rd element
(index 2), adding each item to the total until you reach the end of
the array.

Also be aware that all elements of `process.argv` are strings and you
may need to coerce them into numbers. You can do this by prefixing the
property with `+` or passing it to `Number()`. e.g. `+process.argv[2]`
or `Number(process.argv[2])`.

learnyounode will be supplying arguments to your program when you run
`learnyounode verify program.js` so you don't need to supply them
yourself. To test your program without verifying it, you can invoke it
with `learnyounode run program.js`. When you use `run`, you are
invoking the test environment that learnyounode sets up for each
exercise.

var total = 0;

process.argv.forEach(function(val, index, array) {
if(index>1){
total += Number(val);
} }); console.log(total);

官方例子:

var result = 0

for (var i = 2; i < process.argv.length; i++)
result += Number(process.argv[i]) console.log(result)

《node.js开发指南》P59

process 是一个全局变量,即 global 对象的属性。它用于描述当前 Node.js 进程状态
的对象,提供了一个与操作系统的简单接口。通常在你写本地命令行程序的时候,少不了要
和它打交道。下面将会介绍 process 对象的一些最常用的成员方法。
 process.argv是命令行参数数组,第一个元素是 node,第二个元素是脚本文件名,
从第三个元素开始每个元素是一个运行参数。

nodeschool.io 2的更多相关文章

  1. nodeschool.io 4

    ~~ MY FIRST ASYNC I/O! ~~ Write a program that uses a single asynchronous filesystem operationto rea ...

  2. nodeschool.io 3

    ~~ MY FIRST I/O! ~~ Write a program that uses a single synchronous filesystem operation toread a fil ...

  3. nodeschool.io 10

    ~~ TIME SERVER ~~ Write a TCP time server! Your server should listen to TCP connections on port 8000 ...

  4. nodeschool.io 9

    ~~ JUGGLING ASYNC ~~ 其实就是一个循环,在循环里面输出的顺序,和排列后在外面的顺序不一样,这是为什么呢? 用第三方async包,直接报错了…… This problem is th ...

  5. nodeschool.io 8

    ~~ HTTP COLLECT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the ...

  6. nodeschool.io 7

    ~~ HTTP CLIENT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the f ...

  7. nodeschool.io 6

    ~~ MAKE IT MODULAR ~~ This problem is the same as the previous but introduces the concept ofmodules. ...

  8. nodeschool.io 5

    ~~ FILTERED LS ~~ Create a program that prints a list of files in a given directory,filtered by the ...

  9. NODESCHOOL

    来源:https://nodeschool.io/zh-cn/ 核心基础课程(Core) javascripting 学习 JavaScript 语言的基础,无需任何编程经验 npm install ...

随机推荐

  1. C#正则表达式获取组名,按照组名输出匹配内容

    最近写了个正则表达式匹配的工具,可以按照组名输出匹配内容,还是挺方便的,代码留存一下,以后用的话,直接copy了. Regex regex = new Regex(this.textBoxRegex. ...

  2. SQL中添加远程服务器连接

    EXEC sp_addlinkedserver 'Testserver','','SQLOLEDB','192.168.1.221' EXEC sp_addlinkedsrvlogin 'Testse ...

  3. jquery之insertBefore(),insertAfter(),prependTo(),appendTo()用法详解

    导航: 1,insertBefore(),insertAfter(),prependTo(),appendTo()这四个函数用法几乎一样 2, 与之相对的有四个函数:Before(),After(), ...

  4. error: command 'gcc' failed with exit status 1 的解决办法

    yum install gcc python-devel 之前yum install gcc* 了 所以没成功. wget http://prdownloads.sourceforge.net/doc ...

  5. [JAVA设计模式]第四部分:行为模式

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  6. 问题处理:找不到Pch预编译文件?

    提醒:Xcode6之后就不再自动创建Pch预编译文件 在Xcode6之前,创建一个新工程xcode会在Supporting files文件夹下面自动创建一个“工程名-Prefix.pch”文件,也是一 ...

  7. LTE Module User Documentation(翻译3)——仿真输出

    LTE用户文档 (如有不当的地方,欢迎指正!) 6 仿真输出 ns-3 LTE 模型当前支持输出 PHY, MAC, RLC 和 PDCP 级别的 Key Performance Indicators ...

  8. iOS - UINavigationController

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UINavigationController : UIViewController @available(iOS 2 ...

  9. Linux crontab 定时任务

    http://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/crontab.html 19. crontab 定时任务 通过crontab 命令,我们 ...

  10. 【Todo】Java新技术学习笔记-from某技术分析

    看到这篇文章:http://mt.sohu.com/20160806/n462923089.shtml <十余年技术大牛告诉你,这些Java新技术不可错过> 虽然讲的比较泛,但是里面提到的 ...