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 each first letter of a string:
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 first argument contains all letters from 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 

remove all falsy values from an array:
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]

remove 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] 

returns the lowest index at which the argument will be inserted in 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 cipher:==== function rot13(str) { // I DID IT!! ^^  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");  next item code code code ''' next item''' code code code  next item code code code

next item code code code  next item code code code ''' 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 <p style="font-weight:normal;"> <p style="font-weight:normal;">next item code code code