Skip to content

Instantly share code, notes, and snippets.

View kevjose's full-sized avatar
👨‍💻
zinging

kevin kevjose

👨‍💻
zinging
View GitHub Profile
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
We are using node.js <script>document.write(process.version)</script>.
</body>
</html>
var greeting = 'Hello World';
console.log(greeting);
var calculatePercentile = function(arr){
for (var i = 0; i < arr.length; i++) {
var count = 0;
var start = i;
if (i > 0) {
while (i > 0 && arr[i].value == arr[i - 1].value) {
count++;
i++;
}
}
/* Remove http:// or https:// or www. from url */
var protomatch = /^(https?):\/\/(www\.)?/;
var url ="https://www.google.com";
var b = url.replace(protomatch, '');
console.log(b); //google.com
(function theLoop (i) {
setTimeout(function () {
alert("Cheese!");
if (--i) { // If i > 0, keep going
theLoop(i); // Call the loop again, and pass it the current value of i
}
}, 3000);
})(10);
var matchString = "/ol?link=http://www.facebook.com/flipkart-practo/kevin-kasrt";
var res = matchString.match(/facebook\.com\/(.+)\/+/);
console.log(res[1]);
@kevjose
kevjose / percentile.js
Created May 12, 2016 17:28 — forked from IceCreamYou/percentile.js
Utility functions to calculate percentiles and percent ranks in a JavaScript array.
// Returns the value at a given percentile in a sorted numeric array.
// "Linear interpolation between closest ranks" method
function percentile(arr, p) {
if (arr.length === 0) return 0;
if (typeof p !== 'number') throw new TypeError('p must be a number');
if (p <= 0) return arr[0];
if (p >= 1) return arr[arr.length - 1];
var index = arr.length * p,
lower = Math.floor(index),
var reURLInformation = new RegExp([
'^(https?:)//', // protocol
'(([^:/?#]*)(?::([0-9]+))?)', // host (hostname and port)
'(/[^?#]*)', // pathname
'(\\?[^#]*|)', // search
'(#.*|)$' // hash
].join(''));
var href ="http://www.example.com";
if(href[href.length -1]!='/')
href = href.concat('/');
var startups = [{'score':1},{'score':2},{'score':2},{'score':3},{'score':4}];
startups.sort(function(a, b){
return (b.score - a.score);
});
for(var i =0, rank= 1, j=1; i <startups.length; i++){
startups[i].rank = rank;
if(startups[i+1] && startups[i].score != startups[i+1].score){
rank = rank+j;
j++;
}
// to clone one collection to other
db.demo1.find().forEach( function(x){db.demo2.insert(x)} );
// to remove dupliactes based on a key
db.demo1.find({}, {field:1}).sort({_id:1}).forEach(function(doc){
db.demo1.remove({_id:{$gt:doc._id}, field:doc.field});
})
//to find documents added today
var d = new Date()