New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modulo #14078
Labels
new-challenge
Propose a new challenge, a PR will be auto generated
Comments
its-lee
added
the
new-challenge
Propose a new challenge, a PR will be auto generated
label
Aug 5, 2022
#14079 - Pull Request updated. |
Here's one way to do it: type ToArray<N extends number, _Result extends number[] = []> =
_Result extends { length: N }
? _Result
: ToArray<N, [..._Result, any]>;
type Subtract<A extends any[], B extends any[]> =
A extends [...infer C, ...B]
? C
: never;
type ModulusOfArray<A extends any[], N extends any[]> =
Subtract<A, N> extends never
? A['length']
: ModulusOfArray<Subtract<A, N>, N>;
type Modulus<A extends number, N extends number> = ModulusOfArray<ToArray<A>, ToArray<N>>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Info
Basic info of your challenge questions,
Question
Javascript and a lot of other programming languages support the modulo (also know as remainder) operator
%
which returns the remainder when an integer is divided by another integer. For Javascript's implementation - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder.Here's some examples:
You do not need to worry about the case when the left or right hand sides are negative.
Template
This is the template for challengers to start the coding. Basically, you just need to change the name of your generic/function and leave to implementation
any
.Test Cases
Provide some test cases for your challenge, you can use some utils from
@type-challenges/utils
for asserting.The text was updated successfully, but these errors were encountered: