diff --git a/01-js/medium/times.js b/01-js/medium/times.js index eb125cc2..d9e7f377 100644 --- a/01-js/medium/times.js +++ b/01-js/medium/times.js @@ -1,12 +1,12 @@ -/* -Write a function that calculates the time (in seconds) it takes for the JS code to calculate sum from 1 to n, given n as the input. -Try running it for -1. Sum from 1-100 -2. Sum from 1-100000 -3. Sum from 1-1000000000 -Hint - use Date class exposed in JS -*/ + function calculateTime(n) { - return 0.01; -} \ No newline at end of file + var startTime=performance.now(); + var sum=0; + for(var i=1;i<=n;i++){ + sum+=i; + } + var endTime=performance.now(); + var timePassed=(endTime-startTime)/1000; + return timePassed; +}