Created
April 15, 2019 17:59
-
-
Save sagar-more/227650115feec23d0eaeb156ed367a43 to your computer and use it in GitHub Desktop.
Sum of all numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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