User:Krankuba

===the simplest, library-free code for implementing array intersections: === ​var arr = [1, 2, 3, 4, 5, 6, 7, 9]; var arr2 = [0, 3, 6, 7, 8, 9, 10]; arr.filter(function(n) {    return arr2.indexOf(n) != -1; }); // returns [3, 6, 9]

reverse a string:
var str = "something"; var strReversed = str.split().reverse.join(); // returns strReversed  = gnihtemos

sum all array elements:
var arr = [1, 2, 3, 4]; var arrTotal = arr.reduce(function(previousNum, currentNum) {      return previousNum + currentNum;     }); // returns arrTotal = 10

check for palindromes: function palindrome(str) { var clearString = str.toLowerCase.replace(/[^0-9a-z]/gi, ).split(); for (var i = 0; i < clearString.length/2; i++) { if (clearString[i] !== clearString[clearString.length -1 -i]) { return false; }   }   return true; } function palindrome2(str) { str = str.replace(/\W/g, ).toLowerCase; return (str == str.split().reverse.join()); } palindrome("almostsomla"); // returns true

sort numbers: arrNumbers = arrNumbers.sort(function (a, b) { return b - a;  }); //1,2,3,11,20...

capitalize every first letter:
function titleCase(str) { var arr = str.toLowerCase.split(' '); for (var i in arr) { arr[i] = arr[i].charAt(0).toUpperCase + arr[i].substring(1); }  return arr.join(' '); } titleCase("I'm a little tea pot lot, but the cot"); // returns I'm A Little Tea Pot Lot...

truncate a string:
function truncateString(str, num) { if (num >= str.length) { return str; }  else if (num >3 && num < str.length) { str2 = str.slice(0, num-3); return str2 + "..."; }   else if (num <= 3) { str2 = str.slice(0, num); return str2 + "..."; } } truncateString("Absolutely Longer", 2); // returns Ab...

splits  an  array into groups with the size of the second argument: function chunkArrayInGroups(arr, size) { var arr2 = []; var arrLength = arr.length; for (var i = 0; i < arrLength; i += size) { arr2.push(arr.splice(0, size)); }  return arr2; } chunkArrayInGroups(["a", "b", "c", "d"], 2); // returns "a","b"],["c","d"

checks if the first argument contains all the letters of the second argument:
function mutation(arr) { var firstElement = arr[0].toLowerCase; var secondElement = arr[1].toLowerCase; var arr2 = secondElement.split(); for (var i = 0; i < secondElement.length; i++) { if (firstElement.indexOf(arr2[i]) === -1) { return false; }  }    return true; } mutation(["Mary", "Army"]); // returns true

   removes  all falty characters from a string: function bouncer(arr) { var arr2 = []; for (i = 0; i < arr.length; i++) { if (typeof arr[i] != 'undefined' && arr[i]) { arr2.push(arr[i]); }  }   return arr2; } bouncer([1, null, NaN, 2, undefined]); // returns [1, 2]

removes all elements from the initial array that are present as arguments: function destroyer(arr) { var argumentsArray = []; for (var i = 1; i < arguments.length; i++) { argumentsArray.push(arguments[i]); }  function seekAndDestroy(arr) { return argumentsArray.indexOf(arr) === -1; }   return arr.filter(seekAndDestroy); } destroyer([1, 2, 3, 1, 2, 3], 2, 3, "tree"); // returns [1, 1]

 the lowest index  at which the argument will be inserted into the array: function getIndexToIns(arr, num) { // Find my place in this sorted array. var iBelongHere = 0; //defining compare function, so 2, 5, go before 10 function compareNumbers(a, b) { return a - b;  } //checking if the num argument is already in the array and added if not if ($.inArray(num, arr) === -1) { arr.push(num); }  arr.sort(compareNumbers); for ( i = 0; i < arr.length; i++) { if ( arr[i] === num ) { iBelongHere = i;     } }  return iBelongHere; } getIndexToIns([10, 20, 40, 50], 30); // returns 2 decode a string using ROT13 cypher : function rot13(str) { var arr = []; var n = 0; for (i = 0; i < str.length; i++) { n = str.charCodeAt(i); // n becomes a number if (n >= 65 && n <= 77) { // ABCDEFGHIJKLM + 13 n = n + 13; }    else if (n >=78 && n <= 90) {  // NOPQRSTUVWXYZ - 13 n = n - 13; }    n = String.fromCharCode(n);  // n becomes a string again arr.push(n); //  n goes into a new decrypted string }  return arr.join(); } rot13("SERR PBQR PNZC");

returns the sum of the two arguments and all numbers between them: function sumAll(arr) { var min = Math.min(arr[0], arr[arr.length-1]); var max = Math.max(arr[0], arr[arr.length-1]); for (i = min + 1; i < max; i++) { arr.push(i); }  function getSum(total, num) { return total + num; }  return arr.reduce(getSum); } sumAll([5, 10]); // returns 45

returns the difference between two arrays: function diffArray(arr1, arr2) { var newArr = []; var idx; for (i = 0; i < arr1.length; i++) { idx = arr2.indexOf(arr1[i]); if (idx == -1) { // if 1st array item is not in 2nd array newArr.push(arr1[i]); //add it to the new array }      }         for (i = 0; i < arr2.length; i++) { idx = arr1.indexOf(arr2[i]); if (idx == -1) { // if 1st array item is not in 2nd array newArr.push(arr2[i]); //add it to the new array }      }     return newArr; } diffArray([1, "calf", 3, "piglet", 6, 234], [234, 7, "filly", 6]);

converts the given number into a roman numeral: function convertToRoman(num) { var roman = ""; var romanNumeral = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]; var numbers = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]; for(var i = 0; i < numbers.length; i++) { while(num >= numbers[i]) { roman = roman + romanNumeral[i]; num = num - numbers[i]; }   }   return roman; } convertToRoman(36);

returns an array of all objects that have matching property and value pairs (second argument): function whatIsInAName(collection, source) { var arr = []; var sourceKeys = Object.keys(source); return collection.filter(function (obj) {      for(var i = 0; i < sourceKeys.length; i++) {         if(!obj.hasOwnProperty(sourceKeys[i]) || obj[sourceKeys[i]] !== source[sourceKeys[i]]) {           return false;         }       }       return true;     }); } whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

search and replace: function myReplace(str, before, after) { if (before.charAt(0) == before.charAt(0).toUpperCase) {    var arr = after.split(''); arr[0] = arr[0].toUpperCase; after = arr.join(''); }   return str.replace((before), after); } myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped"); next item code code code  <p style="font-weight:normal;">next item code code code <p style="font-weight:normal;"> <p style="font-weight:normal;">next item code code code <p style="font-weight:normal;"> <p style="font-weight:normal;">next item code code code