Node.js Tutorial - BabySteps - this is javascript?

Node.js Tutorial - BabySteps - this is javascript?
0

#1

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?


#2

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];


#3

So what is the output in both cases?


#4

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


#5

process.argv is an array of strings.


#6

“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.


#7

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.


#8

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.


#9

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.


#10

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:


#11
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);

#12

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.


#13

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


#14

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


#15

MDN


#16

Awesome! Thanks, Ben.