Skip to content

Instantly share code, notes, and snippets.

@prmishra
Last active May 26, 2019 14:44
Show Gist options
  • Save prmishra/d097c0a4ea85338bb7489df433734712 to your computer and use it in GitHub Desktop.
Save prmishra/d097c0a4ea85338bb7489df433734712 to your computer and use it in GitHub Desktop.
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 (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))
// node script.js 1 2 3 4
@sagar-more
Copy link

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

@muhammad-amin-8102
Copy link

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 (item) {
    sum += Number(item);
	return add;
}

};

var hrTime = process.hrtime()
start_time = hrTime[0] * 1000000 + hrTime[1] / 1000
while(add = add()) {}
var hrTime = process.hrtime()
end_time = hrTime[0] * 1000000 + hrTime[1] / 1000

console.log('time ' + Math.round(end_time - start_time))

// node script.js 1 2 3 4

@ashvini-maurya
Copy link

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

@Piyush-85
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment