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 ...
随机推荐
- @RequestMapping测试各种访问方式
这里访问WEB-INF目录下的页面,这个还不知道有没有类似struts2那样的通配符来可以访问不同的action,不同的method,不同 的页面,用户则很爽,有的话求告知,而且我还有一个问题就是配置 ...
- [原创]AM3352 + TPS65910 调试方法+调试记录
时间:20160912 一.电源时序排查 1.保证正确的上电时序,一般都会在CPU数据手册中提到.通常不会规定具体的上电先后时间的要求,多数情况下会要求前一个电平上升到90%满幅度之后才可以有下一个电 ...
- Mybatis+struts2+spring整合
把student项目改造成ssm struts2 +mybatis+spring 1,先添加spring支持:类库三个,applicationContext.xml写在webinf下四个命名空间,监 ...
- SQL Server 字符串处理
) SET @str='AP-FQC-2014072300004' --获取指定字符第一次出现的位置 SELECT PATINDEX('%-%',@str) --返回:3 --获取指定字符第一次出现的 ...
- iOS问题处理:如何在Mac下显示Finder中的所有文件
摘自:http://www.cnblogs.com/elfsundae/archive/2010/11/30/1892544.html 在Unix下工作,你可能需要处理一些“特殊“文件或文件夹,例如/ ...
- Linux基础※※※※如何使用Git in Linux(一)
参考资料: 1. https://www.linux.com/learn/tutorials/796387-beginning-git-and-github-for-linux-users/ 2. h ...
- FLASH CC 2015 CANVAS 中 gotoAndStop、gotoAndPlay() 不起作用
哎 话不多说先看我的代码: //舞台上 放着sp0.sp1....sp8,9个mc,每个mc都有几帧, //帧上有如下代码 var S=this; S.stop() inIt1();//not wor ...
- iOS - MVVM 架构模式
1.MVVM 从字面意思来理解,MVVM 即 Modal View ViewModel(模型 视图 视图模型).MVC 是一个用来组织代码的权威范式,也是构建 iOS App 的标准模式.Apple ...
- iOS企业版打包(转载)
转自 http://www.cnblogs.com/shenlaiyaoshi/p/5472474.html 神来钥匙-陈诗友 iOS 企业版 打包 使用 iOS 企业版的证书发布应用可以跳过 A ...
- poj1873The Fortified Forest
链接 居然是WF的水题~ 二进制枚举砍哪些树,剩余的树围成一个凸包. 因为传数组WA了两发,忘记修改排序数组中的p[0]; #include <iostream> #include< ...