Last active
January 21, 2020 23:25
-
-
Save bekliev/84c553c2ad385260f37c2280da5aede7 to your computer and use it in GitHub Desktop.
Solution to problem on https://medium.com/javascript-in-plain-english/microsoft-online-assessment-questions-js-f68ecdb6e927
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
/* | |
* Try to copy&paste to your browser's console | |
* OR open this image: https://user-images.githubusercontent.com/4066435/72851941-f7ec8f00-3ca4-11ea-9840-29511325a772.png | |
*/ | |
function solution(str, maxEquals = 2) { | |
let result = '' | |
let equals = 0 | |
for (const i in str) { | |
const prev = str.charAt(i - 1) | |
const curr = str.charAt(i) | |
if (prev === curr) equals++ | |
else equals = 0 | |
if (equals >= maxEquals) continue | |
result += curr | |
} | |
return result | |
} | |
solveAndLog('eedaaad') | |
solveAndLog('xxxtxxx') | |
solveAndLog('uuuuxaaaaxuuu') | |
solveAndLog('abbbbbcccddddeeee', 1) | |
solveAndLog('') | |
function solveAndLog(str, maxEquals) { | |
console.log({got: str, result: solution(str, maxEquals)}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's the result of the code above: