exercise 2

32 views
Skip to first unread message

Stephany Prodromos

unread,
Aug 10, 2017, 2:19:50 PM8/10/17
to nodejs
Hi guys I just started learning node today, and am a little confused as to how to go about the second exercise....

at first i had:

var num1 = +process.argv[2];
var num2 = +process.argv[3];
var result = num1 + num2;

console.log(result);

...but this will only work for 2 numbers. any ideas on how to loop through process.argv to account for multiple number inputs??

DaneiL

unread,
Aug 13, 2017, 7:25:35 PM8/13/17
to nod...@googlegroups.com
Have you tryed for?

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscribe@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/6ea4fd6a-acb3-497e-898b-ff7b32a99f2c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
[]'s

John Shaver

unread,
Aug 14, 2017, 4:35:01 AM8/14/17
to nod...@googlegroups.com
You can use a for loop for this:


Use the initialization expression to set the index you want to start at (var i = 2) then test for the end of the array (i < process.argv.length) in the condition expression, increment the index in the "final" expression, and add the value at each index to a running total in the block statement.

Hope that helps. I left out the specific code so you could learn it on your own, but if you just want to see the code that does it, let me know.

-John


Reply all
Reply to author
Forward
0 new messages