Last active
June 23, 2019 08:40
-
-
Save emanuel-sanabria-developer/f704ac14a27b228cf99b902f10e78eac to your computer and use it in GitHub Desktop.
Coding challenges
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
// 1 - Palindrome | |
const palindrome = | |
"A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal-Panama!"; | |
const nonPalindrome = "levels, manaini"; | |
const isPalindrome = (str: string) => { | |
const letters = str.replace(/\W/g, "").toLocaleLowerCase(); | |
return ( | |
letters === | |
letters | |
.split("") | |
.reverse() | |
.join("") | |
); | |
}; | |
// console.log(isPalindrome(palindrome)); | |
// console.log(isPalindrome(nonPalindrome)); | |
// 2 - Curried sum | |
const sum = (n1: number, n2?: number) => | |
n2 !== undefined ? n1 + n2 : (n3: number) => n1 + n3; | |
// console.log(sum(2, 2)); | |
// console.log(sum(2)(2)); | |
// 3 - | |
const domCrawler = (node: HTMLElement, callback: VoidFunction) => { | |
if (node.hasChildNodes) { | |
node.childNodes.forEach(child => { | |
console.log(child); | |
if (child.hasChildNodes) { | |
domCollector(child, callback); | |
} else { | |
callback(child); | |
} | |
}); | |
} else { | |
callback(child); | |
} | |
}; | |
// domCrawler(document.getElementById("app"), console.log); | |
// | |
function binaryGap(num) { | |
// write your code in JavaScript (Node.js 8.9.4) | |
const binary = num.toString(2); | |
const binaryGaps = binary.match(/(0+)(?=1)/g); | |
if (!binaryGaps) { | |
return 0; | |
} | |
const largestGap = binaryGaps.reduce( | |
(acc, b) => (b.length > acc ? b.length : acc), | |
0 | |
); | |
return largestGap; | |
} | |
//console.log(binaryGap(10301)); | |
const oddOccurrencesInArray = A => { | |
let odd; | |
A.sort(); | |
const arrayLength = A.length; | |
for (let index = 0; index < arrayLength; index+=2) { | |
if (A[index] !== A[index + 1]) { | |
odd = A[index]; | |
break; | |
} | |
} | |
return odd; | |
}; | |
console.log(oddOccurrencesInArray4([9, 9, 7, 3, 3])); | |
const cyclicRotator = (a, k) => { | |
const arrayLength = a.length; | |
const posRotation = k % arrayLength; | |
if (!posRotation) { | |
return a; | |
} | |
return [...a.slice(-posRotation), ...a.slice(0, -posRotation)]; | |
}; | |
console.log(cyclicRotator([1, 2, 3, 4, 5, 6], 7)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment