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 ...
随机推荐
- ord函数-php
摘录自http://php.net/manual/zh/function.ord.php (PHP 4, PHP 5, PHP 7) ord — 返回字符的 ASCII 码值 说明 int ord ( ...
- JQuery多媒体插件jQuery Media Plugin使用详解
malsup jquery media plugin 该插件可以播放多种类型的多媒体文件包括:Flash, Quicktime, Windows Media Player, Real Player, ...
- 【leetcode❤python】Ransom Note
#-*- coding: UTF-8 -*- class Solution(object): def canConstruct(self, ransomNote, magazine): ...
- UPC 2170 D Equal Is Not Really Equal (欧拉路径)
题目链接:http://acm.upc.edu.cn/problem.php?id=2170 题意:给出一个字符串,比如ABACA,在这个串里,AB.BA.AC.CA各出现一次.若存在另外一个串,里面 ...
- JAVA 实战练习
1.判断变量是否为奇数偶数. package com.JAVA; import java.util.Scanner; public class text { public static void ma ...
- 。JavaSE------初识Java
我的老师告诉我,命运眷顾有志者,天道酬勤. 有时在梦里幻想的再多终究也只是梦, 不如脚踏实地一步步往前走来的踏实. ------------------------------------------ ...
- GZFramwork快速开发框架演练之会员系统(二)添加字典模块
开始前请先阅读 GZFramwork快速开发框架之窗体设计说明 第一步:准备模块图片 图片为2张大小分别为16x16和32x32,放在\Debug\images目录下 因为会员管理模块并不多 ...
- iOS 框架收集
检测硬件设备信息 https://github.com/Shmoopi/iOS-System-Services
- linux 命令行模式下,浏览网页
Ubuntu自带最新版的Gnome桌面,拥有大量的服务和桌面应用程序,让您仅通过一张安装光盘就可以体验到无比舒适的操作环境.下文介绍的在ubuntu下使用终端命令行上网的方法. 第一步,需要安装一个名 ...
- 利用[后台]->[类别管理]为文章前后台添加类别名称【转】
原网址:http://blog.csdn.net/yanhui_wei/article/details/7943176 1.给专题添加文章时,可以选择类别: 2.给文章模型.图片模型.下载模型的栏目下 ...