Skip to content

Instantly share code, notes, and snippets.

// Includes functions for exporting active sheet or all sheets as JSON object (also Python object syntax compatible).
// Tweak the makePrettyJSON_ function to customize what kind of JSON to export.
var FORMAT_ONELINE = 'One-line';
var FORMAT_MULTILINE = 'Multi-line';
var FORMAT_PRETTY = 'Pretty';
var LANGUAGE_JS = 'JavaScript';
var LANGUAGE_PYTHON = 'Python';
//From codewars (level 4), inputs an n x n nested array of numbers and outputs an array documenting a clockwise spiraling journey from
//the outer to inner
function snail(array){
var result = [];
for (var entire = 0; entire < array.length + Math.floor(array.length / 2); entire++){
//push the first index of an array in its entirety, then splice it out.
result.push(array[0]);
array.splice(0,1);
/*
Mimics JSON.stringify.
Uses recursion.
*/
function stringifyJSON(obj){
var result = [];
if (obj && typeof obj === 'object'){
for (var key in obj){
if (typeof obj[key] === 'object' && !Array.isArray(obj[key])){
/*
Takes in a positive integer of seconds and returns the number of minutes, hours, days and years.
Works for singular numbers (will return "day" instead of "days")
*/
function getTimeFromSeconds(secs){
if (!secs) return "Now";
var theTime = {
seconds: secs,
minutes: 0,
function sortByFreq(str){
str = str.split("");
//filter out duplicate letters, then map them to an array where the first index is the frequency in str and..
//the second index is the letter. Then sort by the first index (frequency).
var noDuplicatesByFrequency = str.filter((letter, index) => index === str.indexOf(letter)).
map(letter => [str.filter(item => item === letter).length, letter]).sort((a,b) => b[0] - a[0]);
//return noDuplicatesByFreqncy mapped so that the second index (letter) is repeated [first index] (frequency) - joined.
function isSubsequence(s,t){
s = s.split("");
t = t.split("");
for (var letter of s){
if (!t.includes(letter)) return false;
t = t.slice(t.indexOf(letter) + 1);
}
return true;
}