nodeschool.io 2
~~ 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的更多相关文章
- nodeschool.io 4
~~ MY FIRST ASYNC I/O! ~~ Write a program that uses a single asynchronous filesystem operationto rea ...
- nodeschool.io 3
~~ MY FIRST I/O! ~~ Write a program that uses a single synchronous filesystem operation toread a fil ...
- nodeschool.io 10
~~ TIME SERVER ~~ Write a TCP time server! Your server should listen to TCP connections on port 8000 ...
- nodeschool.io 9
~~ JUGGLING ASYNC ~~ 其实就是一个循环,在循环里面输出的顺序,和排列后在外面的顺序不一样,这是为什么呢? 用第三方async包,直接报错了…… This problem is th ...
- nodeschool.io 8
~~ HTTP COLLECT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the ...
- nodeschool.io 7
~~ HTTP CLIENT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the f ...
- nodeschool.io 6
~~ MAKE IT MODULAR ~~ This problem is the same as the previous but introduces the concept ofmodules. ...
- nodeschool.io 5
~~ FILTERED LS ~~ Create a program that prints a list of files in a given directory,filtered by the ...
- NODESCHOOL
来源:https://nodeschool.io/zh-cn/ 核心基础课程(Core) javascripting 学习 JavaScript 语言的基础,无需任何编程经验 npm install ...
随机推荐
- 一、Java语言基础
1.标识符和关键字 标识符是java中必须使用的,具有一定的规则,用来标识类名.变量名.方法名.数组名.文件名等. 例:int i = 100; 变量i就是标识符 ...
- ubuntu12.04安装mysql
首先下载ubuntu 12.04 64位对应的myqsl版本 http://dev.mysql.com/downloads/file/?id=464508 然后按照如下 ...
- Strategy策略模式
策略模式定义了一系列算法,把它们一个个封装起来,并且使它们可相互替换.该模式可使得算法能独立于使用它的客户而变化.Strategy模式是行为模式,正因为他是一种行为模式,所以他不是用来解决类的实例化的 ...
- \n ^ \t的使用
\n是换行符: 使用时需要放在字符串里面,成为字符串的一部分(不要嫌乱,这是我之前犯的错误). 示例: >>>print("I am Guido \n what's yon ...
- poj 2985 The k-th Largest Group 树状数组求第K大
The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8353 Accepted ...
- Redis主从配置详细过程
Redis的主从复制功能非常强大,一个master可以拥有多个slave,而一个slave又可以拥有多个slave,如此下去,形成了强大的多级服务器集群架构.下面楼主简单的进行一下配置. 1.上面安装 ...
- poj2986A Triangle and a Circle&&poj3675Telescope(三角形剖分)
链接 2986是3675的简化版,只有一个三角形. 这题主要在于求剖分后三角形与圆的相交面积,需要分情况讨论. 具体可以看此博客 http://hi.baidu.com/billdu/item/703 ...
- (三)ubuntu学习前传—uboot常见环境变量
1.环境变量如何参与程序运行(1)环境变量有2份,一份在Flash中,另一份在DDR中.uboot开机时一次性从Flash中读取全部环境变量到DDR中作为环境变量的初始化值,然后使用过程中都是用DDR ...
- android下基本json串的生成与解析
以前就用过json串,不过是在java环境下面,如今转移到android环境下,java里面生成解析json串的jar包与android中自带的冲突,所以也只能用安卓自带的. 先前查网上的资料,感 ...
- 使用Mozilla Firefox插件RestClient测试Http API接口
RESTClient是Mozilla Firefox一个用于测试http请求插件.在火狐附加组件里面查询并安装,非常小巧,界面非常简单,使用非常的方便,看下面这张图你就全明白了,希望对新手有帮助! 1 ...