~~  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. 高通平台 lcd driver 调试小结

    一.概述 1.1 简介 本文档主要包括LCD模块的驱动流程分析.Framebuffer相关知识.Gralloc等相关内容,以及LCD调试的一些经验和相关bug的分析和讲解. 1.2  开发环境 And ...

  2. 达人眼中的WINCE网络驱动

    实际上在WinCE上开发网络驱动,比如设计一个NIC驱动, 大多数情况,是从XP移植NDIS Miniport驱动(小端口驱动)到WinCE.什么是ndis?Ndis做什么用的? 什么是minipor ...

  3. Java IO流整理Rick

    Java 流IO部分: Console 控制台信息读取// ----------- Console Begin // ------------ 部分代码  Console cons ;  char[] ...

  4. CP,SCP 命令(包括windows与linux用xshell互传)

    linux之cp/scp命令+scp命令详解 名称:cp 使用权限:所有使用者 使用方式: cp [options] source dest cp [options] source... direct ...

  5. CUBRID学习笔记 19 sql语句1

    创建 欢迎转载 ,转载时请保留作者信息.本文版权归本人所有,如有任何问题,请与我联系wang2650@sohu.com . 过错 create table tableName (字段名 字段类型 pr ...

  6. jQuery:show()方法

    show()方法是jQuery的方法,功能是显示元素,参数是时间,单位是毫秒,例如: Html: <div class="one" id="one"> ...

  7. 教你如何拔取百度地图POI兴趣点

    教你如何拔取百度地图POI兴趣点   通过聚合数据提供的接口,获取百度地图的POI兴趣点,并存储至数据库中. 实现: 1.聚合数据百度POI接口说明 调用聚合数据,首先得注册聚合.聚合数据提供的百度地 ...

  8. JS 命名冲突

    1. JS中全局变量和局部变量重名会导致在指定域内无法取到变量: 2. 取出的结果为undefined;

  9. 使用yum快速部署Oracle安装环境(11g)

    基于Linux安装过Oracle的童鞋们都应该清楚,安装Oracle的确是一件比较费时费力的差事,因为仅仅是前期的rpm包,内核参数,创建用户等等这些个步骤都让那些新手不免眼花缭乱,一不留神,就导致最 ...

  10. Docker-网络基础配置

    从外部访问容器 指定容器端口随机映射主机端口 [root@wls12c /]$ docker run -p -d --name web tomcat /bin/bash -c /root/apache ...