Monday, December 2, 2013

Node code for Ternary and minus combined

saw a very simple solution for learnyounode as following

//console.log(process.argv);
var total = 0;
process.argv.forEach(function(item){
        total += +item ? + item: 0;
});
console.log(total);

this is used to print out summary of command parameters
 fouth line need to be explained
+item is the a minus operation on variable "item" value(not item itself),try to convert item value(String) to a number,if the result is a number,then the ternary operator will be true,since item might be a string,we also use +item to convert item value into a number if it is a string when ternary operator returns true.



No comments:

Post a Comment