Skip to content

Instantly share code, notes, and snippets.

@AviTapp
AviTapp / caesarsCipher.js
Created December 26, 2017 22:20
Write a function which takes a ROT13 encoded string as input and returns a decoded string. All letters will be uppercase. Do not transform any non-alphabetic character (i.e. spaces, punctuation), but do pass them on.
function rot13(r) {
r = r.split("");
var t = "";
for (i = 0; i < r.length; i++)
if (/[^A-Z]/.test(r[i])) t += r[i];
else {
var e = r[i].charCodeAt(0);
e > 77 ? e -= 13 : e += 13, t += String.fromCharCode(e);
}
return t;
@AviTapp
AviTapp / whereDoIBelong.js
Created December 26, 2017 20:21
Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. The returned value should be a number.
function getIndexToIns(n, r) {
var t = r;
for (n.sort(function(n, i) {
return n - i;
}), i = 0; i < n.length; i++)
if (r <= n[i]) {
r = i;
break;
}
return t == r && (r = n.length), r;
@AviTapp
AviTapp / seekAndDestroy.js
Created December 26, 2017 18:49
You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.
function destroyer(r) {
var n = arguments;
for (i = 1; i < n.length; i++) {
var t = n[i];
r = r.filter(function(r) {
return r != t;
});
}
return r;
}
@AviTapp
AviTapp / falsyBouncer.js
Last active December 26, 2017 19:13
Remove all falsy values from an array.
function bouncer(n) {
return n = n.filter(function(n) {
var r = [!1, null, 0, "", void 0, NaN];
for (i = 0; i < r.length; i++) return n != r[i] ? n : void 0
})
}
@AviTapp
AviTapp / chunkyMonkey.js
Last active December 26, 2017 19:12
Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a two-dimensional array.
function chunkArrayInGroups(r, n) {
var u = [],
c = 0,
e = n,
h = [];
for (i = 0; i < r.length;) u = r.slice(c, e), h.push(u), c += n, e += n, i += n;
return h
}
@AviTapp
AviTapp / mutations.js
Last active December 26, 2017 19:11
Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.
function mutation(r) {
r[0] = r[0].toLowerCase(), r[1] = r[1].toLowerCase();
var t = 0,
e = r[1];
for (e = e.split(""), i = 0; i < e.length; i++) {
var n = e[i];
if (!(r[0].indexOf(n) >= 0)) return !1;
t = 1
}
return 0 !== t
@AviTapp
AviTapp / slasherFlick.js
Last active December 26, 2017 19:10
Return the remaining elements of an array after chopping off n elements from the head.
function slasher(e, n) {
return e = e.slice(n)
}
@AviTapp
AviTapp / truncateAString.js
Last active December 26, 2017 19:09
Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a ... ending.
function truncateString(n, t) {
var e = n.length;
return e == t || e < t ? n : e < 3 || t < 3 ? (n = n.slice(0, t), n += "...") : (n = n.slice(0, t - 3), n += "...")
}
@AviTapp
AviTapp / repeatAStringRepeatAString.js
Last active December 26, 2017 19:08
Repeat a given string (first argument) num times (second argument). Return an empty string if num is not a positive number.
function repeatStringNumTimes(r, e) {
var n = r;
if (!(e > 0)) return "";
for (i = --e; i > 0; i--) r += n;
return r
}
@AviTapp
AviTapp / ConfirmTheEnding.js
Last active December 26, 2017 19:07
Check if a string (first argument, str) ends with the given target string (second argument, target). [Avoiding using .endsWith()]
function confirmEnding(n, r) {
var t = r.length;
return r == n.substr(-t)
}