~~  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. Python3基础 type获取变量的类型

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  2. js友好提示是否继续,post提交

    <script type="text/javascript"> function delcheck(qId,typeid) { if (!confirm('确定删除吗? ...

  3. 中国特色社会主义的体制中有这样的现象:地方省政府要坚持党的领导和按 照国务院的指示进行安全生产。请编写一个java应用程序描述上述的体制现象。

    package a; public interface CentralPartyCommittee { void partyLeader(); } package a; public abstract ...

  4. web应用中web.xml配置详解

    Web.xml常用元素 <web-app> <display-name></display-name>定义了WEB应用的名字 <description> ...

  5. Django的Many-to-Many(多对多)模型

      Django的Many-to-Many(多对多)模型 日期:2012-05-05 |  来源:未知 |  作者:redice |  人围观 |  1 人鼓掌了! 鲲鹏Web数据抓取 - 专业Web ...

  6. STORM_0008_Structure-of-the-codebase_Storm的代码库的结构

    http://storm.apache.org/releases/1.0.1/Structure-of-the-codebase.html Structure of the codebase 源码分成 ...

  7. iOS - Swift NSCalendar 日历

    前言 public class NSCalendar : NSObject, NSCopying, NSSecureCoding NSCalendar 对世界上现存的常用的历法进行了封装,既提供了不同 ...

  8. iOS - Plist 数据解析

    前言 NS_AVAILABLE(10_6, 4_0) @interface NSPropertyListSerialization : NSObject 如果对象是 NSArray 或 NSDicti ...

  9. JavaScript基础知识之——Location 对象详解

    属性 描述 location.hash 设置或取得 URL 中的锚 location.host 设置或取得 URL 中主机(包括端口号) location.hostname 设置或取得 URL 中的主 ...

  10. [转载] 深入 nginx 架构

    原文: http://www.cnbeta.com/articles/402709.htm 了解 nginx 架构帮助我们学习如何开发高性能 web 服务. 为了更好地理解设计,你需要了解NGINX是 ...