Skip to content

Instantly share code, notes, and snippets.

@sandeshdamkondwar
Last active August 29, 2015 14:17
Show Gist options
  • Save sandeshdamkondwar/7f2aba6538cff7e0d37d to your computer and use it in GitHub Desktop.
Save sandeshdamkondwar/7f2aba6538cff7e0d37d to your computer and use it in GitHub Desktop.
JavaScript tricks
// 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= {"<": "&lt;", ">": "&gt;","&": "&amp;", """: "&quot;"};
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