Node.js Tutorial - BabySteps - this is javascript?

Been heavy into javascript since starting FCC back in May.

Now I have made it to the node.js tutorial and I am not recognizing some syntax.

The js that I have commented out below is the javascript I have come to know for incrementing a variable.
But now, not only does it seem Number(int) is the only thing that will work here, but also node.js treats it as if “sum” was an arra and I had written sum.push(process.argv[i]); !!!

var sum = 0;

for (var i = 2; i < process.argv.length; i++) {

    // sum = sum+(process.argv[i]); this treats sum as an array and pushes (process.argv[i]); to the array!!!???!!!
    sum += Number(process.argv[i]);
}

console.log(sum);

Is this still javascript or does node.js use its own particular flavor of js?

I’m pretty sure it should be written without the brackets as you are just pointing to a value at index ‘i’

sum = sum + process.argv[i];

1 Like

So what is the output in both cases?

How did you get through all the sections up until the backend projects so quickly?

process.argv is an array of strings.

“Quickly”? It’s been seven months. I started back in May!

I have skipped nothing, by the way. Started at the very first front-end exercise and have worked every single one.

React.js was probably the most difficult to “get” and took a long time to work through.

No, that treats it like an array as well rather than incrementing an integer

var sum = 0;

for (var i = 2; i < process.argv.length; i++) {

  // sum = sum+(process.argv[i]); this treats sum as an array and pushes (process.argv[i]); to the array!!!???!!!
  //sum += Number(process.argv[i]);
  sum = sum+process.argv[i];
}

console.log(sum);

Going to check out Jenov’s answer when I get back to this on my lunchbreak - I think that may be where I am going wrong. Just relieved to not see anyone saying “Node.js has its own flavor of js” Looks like the error is once again somewhere between chair and keyboard.

The output in both cases is “0123”

It’s only when I utilize Number(int) that I get the expected outcome, “6.”

Again, I will be taking another look at this later based on Jenovs’ response - got a feeling that is where the problem is.

So it doesn’t return an array. As @jenovs said, process.argv is an array of strings, so you are just adding several strings together.

2 Likes

sum = sum + (process.argv[i]); will return 0123 because if you try to add number and string JS coerce number to string and then just concatenate them together.

sum += Number(process.argv[i]); will return 6 because Number() tries to convert string to number.

Bonus:
sum +=+ process.argv[i]; will also return 6 :sunglasses:

2 Likes
var sum = 0;

for (var i = 2; i < process.argv.length; i++) {

  // sum = sum+(process.argv[i]); this treats sum as an array and pushes (process.argv[i]); to the array
  //sum += Number(process.argv[i]);  Works!
  //sum = sum+process.argv[i];  this treats sum as an array and pushes (process.argv[i]); to the array
  sum +=+ process.argv[i]; //works!
}

console.log(sum);

Hey JS also has new syntax for converting strings into integers: ‘+’.

So if process.argv[i] = “6”, then +process.argv[i] = 6.

I haven’t been able to find documentation on it, but I learned it doing Angular 2’s tutorials to convert url params into integers.

That’s what second + in my bonus example does :nerd_face:

Ah I see. Did you find any documentation on that syntax? Or is it just using addition to coerce the string into an integer?

MDN

1 Like

Awesome! Thanks, Ben.