Skip to content

Instantly share code, notes, and snippets.

@heygema
Last active April 4, 2018 04:53
Show Gist options
  • Save heygema/a0c0fc5ef05dbe90b187bc3f16b4b516 to your computer and use it in GitHub Desktop.
Save heygema/a0c0fc5ef05dbe90b187bc3f16b4b516 to your computer and use it in GitHub Desktop.
KF coding challenge
// 1. getAverage;
let getAverage = array => array.reduce((a, b) => a + b, 0) / array.length
// 2. DigitsExplode
let explode = s => s.replace(/[0-9]/g, d => d.repeat(d));
// 3. Parse string to key value pairs
function wordsToObject(input) {
let arr = input.split(" ");
const L = arr.length;
let num = 0;
let result = [];
let obj = {};
for (let i=0;i<L;i++) {
if (i % 2 === 0) {
obj.name = arr[i];
} else {
obj.id = arr[i];
}
if (obj.name && obj.id) {
result[num] = `{name : '${obj.name}', id : '${obj.id}'}`;
obj = {}
num += 1;
}
}
let stringed = result.join(', ')
return `[${stringed}]`;
}
// 4. Sum of Multiples
// 5. Divisors
// 6. Angram Detection
const isAnagram=(a,b)=>a.length===b.length?((a,b)=>(a+b).toLowerCase().split('').filter((x)=>(a+b).toLowerCase().split('').filter((c)=>c===x).length===1).length===0)(a,b):false;
// 10. to Snake Case
const toSnake = (word) => {
let result = '';
for (let w of word) {
if (w === w.toUpperCase()) {
result += `_${w}`;
} else {
result += w;
}
}
return result.toLowerCase();
}
(* 1. getAverage *)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment