This file contains hidden or 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
| <!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> |
This file contains hidden or 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 greeting = 'Hello World'; | |
| console.log(greeting); |
This file contains hidden or 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 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++; | |
| } | |
| } |
This file contains hidden or 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
| /* 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 |
This file contains hidden or 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
| (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); |
This file contains hidden or 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 matchString = "/ol?link=http://www.facebook.com/flipkart-practo/kevin-kasrt"; | |
| var res = matchString.match(/facebook\.com\/(.+)\/+/); | |
| console.log(res[1]); |
This file contains hidden or 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
| // 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), |
This file contains hidden or 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 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('/'); |
This file contains hidden or 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 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++; | |
| } |
This file contains hidden or 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
| // 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() |
OlderNewer