Last active
February 27, 2018 05:50
-
-
Save zetekla/39dc811083d36497dee9acc947ae9dd9 to your computer and use it in GitHub Desktop.
flatten arrays
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
var arr = [[1,2,[3]],4]; | |
function flatten(arr){ | |
return arr.reduce(function(pre,curr){ | |
return pre.concat(curr.constructor === Array ? flatten(curr) : curr); | |
}, []); | |
} | |
console.log(flatten(arr)); | |
function solutionAvgColor (hex1, hex2) { | |
let parseRGB = (hex) => { | |
let bigint = parseInt(hex, 16); | |
return [ (bigint >> 16) & 255 , (bigint >> 8) & 255 , bigint & 255 ] | |
}; | |
let [r1, g1, b1] = parseRGB(hex1); | |
let [r2, g2, b2] = parseRGB(hex2); | |
let rtb = (r1 + r2) / 2; | |
let gtb = (g1 + g2) / 2; | |
let btb = (b1 + b2) / 2; | |
return ( (r, g, b) => ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1) ) (rtb, gtb, btb); | |
} | |
console.log(solutionAvgColor('660000', '000000')); |
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
var str = '1.0.1-alpha.2.39'; | |
console.clear(); | |
console.log('first char', str.slice(0,1)); // 1 | |
console.log('last char', str.slice(-1)); // 9 | |
console.log('grab all regular chars except special chars', str.match(/[(?:\d+|\w+)]/g)); // ["1", "0", "1", "a", "l", "p", "h", "a", "2", "3", "9"] | |
console.log('grab all words except special chars', str.match(/(?:\d+|\w+)/g)); // ["1", "0", "1", "alpha", "2", "39"] | |
console.log('grab special chars', str.match(/[^(?:\d|\w)]/g)); // [".", ".", "-", ".", "."] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment