Last active
August 29, 2015 14:17
-
-
Save sandeshdamkondwar/7f2aba6538cff7e0d37d to your computer and use it in GitHub Desktop.
JavaScript tricks
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
// Get a random number in a specific range | |
function randomInRange(max, min) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
// Get a random item from an array | |
function randomFromArray(items) { | |
return items[Math.floor(Math.random() * items.length)]; | |
} | |
// trim function for string | |
function myTrim(str) { | |
return str.replace(/^s+|s+$/g, ""); | |
} | |
// An HTML escaper function | |
function escapeHTML(text) { | |
var replacements= {"<": "<", ">": ">","&": "&", """: """}; | |
return text.replace(/[<>&"]/g, function(character) { | |
return replacements[character]; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment