Created
June 14, 2017 18:15
-
-
Save misterussell/116ec6afc07fcad1ea173532a80c6490 to your computer and use it in GitHub Desktop.
Codewars Test 1
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
function checkIfArray(arr) { | |
if (arr.constructor === Array ) { | |
return arr; | |
} else return convertToArray(arr); | |
} | |
function convertToArray(string) { | |
return Array.from(string); | |
} | |
function verifyLength(data) { | |
if (data.length === 0) { | |
return 0; | |
} else if (data.length === 1) { | |
return 1 | |
} return data.length; | |
} | |
function verifyDups(array) { | |
// we need to return items in an array only if they do not match the ref point that was just verified | |
return array.filter((ref, i) => { | |
if (ref === array[i+1]) { | |
return null; | |
} else { | |
return ref; | |
} | |
}); | |
} | |
function uniqueInOrder(iterable){ | |
// should work with empty arrays | |
// should work with one element | |
// should work with larger arrays | |
let length = verifyLength(iterable); | |
if (length === 0) { | |
return []; | |
} else if (length === 1) { | |
return checkIfArray(iterable); | |
} else { | |
return verifyDups(checkIfArray(iterable)); | |
} | |
} | |
console.log(uniqueInOrder('AAAABBBCCDAABBB')); | |
console.log(uniqueInOrder('a')); | |
console.log(uniqueInOrder('')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment