Last active
January 19, 2018 18:27
-
-
Save glebcha/3a9509f5a73e13bbb5ea0f4ebd27e4a5 to your computer and use it in GitHub Desktop.
Challenges in a hard way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Find summ of all elements in array => result: 32 | |
var multiArr = [1, '2x', ['3', ['x2', ['10', '10'], '5'], '1x']]; | |
function summ(arr, result) { | |
var result = result || 0; | |
for (var i = 0; i < arr.length; i++) { | |
var number = arr[i]; | |
var notNumber = isNaN(parseInt(number)); | |
var isArray = Object.prototype.toString.call(number) === '[object Array]'; | |
if(!isArray && !notNumber) { | |
result += parseInt(number); | |
} | |
else if(isArray) { | |
result = summ(number, result); | |
} | |
} | |
return result; | |
} | |
summ(multiArr); | |
// Merge letters with defined separator in first argument => result: 'a,b' | |
function mergeLetters(args) { | |
if (!arguments || arguments.length < 2) { | |
throw('Pass delimiter and letters'); | |
} | |
var args = Array.prototype.slice.call(arguments); | |
var delimiter = args.splice(0,1)[0]; | |
return args.join(delimiter); | |
} | |
mergeLetters(',', 'a', 'b'); | |
// Compress letters by occurencies => result: A4B3C2XYZD4E3F3A6B28 | |
var letters = 'AAAABBBCCXYZDDDDEEEFFFAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBB'; | |
function runLengthEncoding(str) { | |
if (str.length === 0 || Object.prototype.toString.call(str) !== '[object String]') { | |
throw('Invalid data'); | |
} | |
var occurencies = 1; | |
var result = ''; | |
for (var i = 0; i < str.length; i++) { | |
var char = str.charAt(i); | |
var nextChar = str.charAt(i + 1); | |
if (char === nextChar) { | |
++occurencies; | |
} else { | |
result += occurencies === 1 ? char : (char + occurencies); | |
occurencies = 1; | |
} | |
} | |
return result; | |
} | |
runLengthEncoding(letters); | |
// Detect whether word or phrase is palindrome | |
var latinWord = 'A man, a plan, a canal -- Panama'; | |
var cyrWord = 'Око'; | |
function isPalindrom(str) { | |
if (!str || Object.prototype.toString.call(str) !== '[object String]') { | |
throw('Pass valid string'); | |
} | |
var clearedString = str.replace(/[^a-z\u0400-\u04FF]/gi, '').toLowerCase(); | |
var middle = Math.floor((clearedString.length - 1) / 2); | |
var index = 0; | |
while(index <= clearedString.length - 1) { | |
var prev = clearedString.charAt(middle-index); | |
var next = clearedString.charAt(middle+index); | |
if(prev !== next) { | |
return false; | |
} | |
else { | |
++index; | |
} | |
} | |
return true; | |
} | |
isPalindrom(word); | |
function flattenFilesystem(fs, output = [], dir) { | |
return fs.reduce((acc, item) => { | |
const isString = Object.prototype.toString.call(item) === '[object String]'; | |
const isObject = Object.prototype.toString.call(item) === '[object Object]'; | |
const rootPath = dir ? `${dir}/${item}` : item; | |
if (isString) { | |
acc.push(rootPath); | |
} else if(isObject) { | |
Object.keys(item).forEach((key) => { | |
let path = dir ? `${dir}/${key}` : key; | |
if (Array.isArray(item[key])) { | |
flattenFilesystem(item[key], output, path) | |
} else { | |
path = dir ? `${dir}/${key}/${item[key]}` : item[key]; | |
acc.push(path); | |
} | |
}); | |
} | |
return acc; | |
}, output); | |
}; | |
const fileStructure = [ | |
'a.js', | |
'b.js', | |
null, | |
false, | |
{ | |
src: [ | |
'index.js', | |
{ | |
js: [ | |
'Home.js', | |
'About.js', | |
], | |
assets: [ | |
{sprites: ['sprite.svg']}, | |
'icons.svg', | |
] | |
} | |
] | |
}, | |
{ | |
routes: [ | |
'index.js' | |
] | |
} | |
]; | |
flattenFilesystem(fileStructure); | |
// Spread any type | |
function spreadAny(arr, arguments) { | |
return [...arr, ...[arguments]]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment