Skip to content

Instantly share code, notes, and snippets.

@karol-majewski
Created October 12, 2021 15:46
Show Gist options
  • Save karol-majewski/890904542baf92242c31e3d7230edfa4 to your computer and use it in GitHub Desktop.
Save karol-majewski/890904542baf92242c31e3d7230edfa4 to your computer and use it in GitHub Desktop.
type NonEmptyArray<T> = [T, ...T[]];
const names: NonEmptyArray<string> = ['Sheldon', 'Leonard', 'Penny', 'Rajesh', 'Howard'];
function main(names: NonEmptyArray<string>, n: number){
let index = n - 1;
while (index >= names.length) {
index = Math.floor((index - names.length) / 2);
}
return names[index];
}
console.log(
main(names, 1),
main(names, 52),
main(names, 7230702951)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment