Skip to content

Instantly share code, notes, and snippets.

@sagar-more
Created April 15, 2019 17:59
Show Gist options
  • Save sagar-more/227650115feec23d0eaeb156ed367a43 to your computer and use it in GitHub Desktop.
Save sagar-more/227650115feec23d0eaeb156ed367a43 to your computer and use it in GitHub Desktop.
Sum of all numbers
var list = process.argv.slice(2);
var sum = 0;
var add = function () {
if (list.length === 0) {
console.log('sum ' + sum);
}
var item = list.pop();
if (!isNaN(item)) {
sum += Number(item);
add();
}
};
var hrTime = process.hrtime()
start_time = hrTime[0] * 1000000 + hrTime[1] / 1000
add();
var hrTime = process.hrtime()
end_time = hrTime[0] * 1000000 + hrTime[1] / 1000
console.log('time ' + Math.round(end_time - start_time) / 1000)
// node script.js 1 2 3 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment