Function that given a number returns the number within a range
up vote
3
down vote
favorite
I am trying to write a function that given a number, it returns the number within the range of 1 to 12.
So, for example, if the input is 1, the output is 1, if the input is 12, the output is 12, if the input is 13, the output is 1, if the input is 14, the output is 2, if the input is 24, the output is 12.
I've tried this so far:
function toRange(number) {
if (number > 12) {
return number % 12
}
return number
}
But I am wondering if there is a way that I can solve this without doing conditionals. I thought of doing return (number + 12) % 12, but that wouldn't work if number is 12, because it would return 0.
javascript modulo
add a comment |
up vote
3
down vote
favorite
I am trying to write a function that given a number, it returns the number within the range of 1 to 12.
So, for example, if the input is 1, the output is 1, if the input is 12, the output is 12, if the input is 13, the output is 1, if the input is 14, the output is 2, if the input is 24, the output is 12.
I've tried this so far:
function toRange(number) {
if (number > 12) {
return number % 12
}
return number
}
But I am wondering if there is a way that I can solve this without doing conditionals. I thought of doing return (number + 12) % 12, but that wouldn't work if number is 12, because it would return 0.
javascript modulo
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I am trying to write a function that given a number, it returns the number within the range of 1 to 12.
So, for example, if the input is 1, the output is 1, if the input is 12, the output is 12, if the input is 13, the output is 1, if the input is 14, the output is 2, if the input is 24, the output is 12.
I've tried this so far:
function toRange(number) {
if (number > 12) {
return number % 12
}
return number
}
But I am wondering if there is a way that I can solve this without doing conditionals. I thought of doing return (number + 12) % 12, but that wouldn't work if number is 12, because it would return 0.
javascript modulo
I am trying to write a function that given a number, it returns the number within the range of 1 to 12.
So, for example, if the input is 1, the output is 1, if the input is 12, the output is 12, if the input is 13, the output is 1, if the input is 14, the output is 2, if the input is 24, the output is 12.
I've tried this so far:
function toRange(number) {
if (number > 12) {
return number % 12
}
return number
}
But I am wondering if there is a way that I can solve this without doing conditionals. I thought of doing return (number + 12) % 12, but that wouldn't work if number is 12, because it would return 0.
javascript modulo
javascript modulo
asked Nov 8 at 11:23
Hommer Smith
8,61937110203
8,61937110203
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
You could use an adjustment for a zero based value and add the adustment at the end.
function toRange(number) {
return (number - 1) % 12 + 1;
}
var i;
for (i = 1; i <= 24; i++) console.log(i, toRange(i));
.as-console-wrapper { max-height: 100% !important; top: 0; }
add a comment |
up vote
0
down vote
maybe something like this:
function toRange(number) {
if (number > 12) {
if (number % 12 === 0)
return number - 12;
return number % 12
}
return number
}
add a comment |
up vote
0
down vote
Can also be thought of like - Get the remainder of number by 12 and return 12 in case remainder is 0.
function toRange(number) {
return number % 12 || 12;
}
for (var i = 1; i <= 24; i++) console.log(i, toRange(i));
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You could use an adjustment for a zero based value and add the adustment at the end.
function toRange(number) {
return (number - 1) % 12 + 1;
}
var i;
for (i = 1; i <= 24; i++) console.log(i, toRange(i));
.as-console-wrapper { max-height: 100% !important; top: 0; }
add a comment |
up vote
2
down vote
accepted
You could use an adjustment for a zero based value and add the adustment at the end.
function toRange(number) {
return (number - 1) % 12 + 1;
}
var i;
for (i = 1; i <= 24; i++) console.log(i, toRange(i));
.as-console-wrapper { max-height: 100% !important; top: 0; }
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You could use an adjustment for a zero based value and add the adustment at the end.
function toRange(number) {
return (number - 1) % 12 + 1;
}
var i;
for (i = 1; i <= 24; i++) console.log(i, toRange(i));
.as-console-wrapper { max-height: 100% !important; top: 0; }
You could use an adjustment for a zero based value and add the adustment at the end.
function toRange(number) {
return (number - 1) % 12 + 1;
}
var i;
for (i = 1; i <= 24; i++) console.log(i, toRange(i));
.as-console-wrapper { max-height: 100% !important; top: 0; }
function toRange(number) {
return (number - 1) % 12 + 1;
}
var i;
for (i = 1; i <= 24; i++) console.log(i, toRange(i));
.as-console-wrapper { max-height: 100% !important; top: 0; }
function toRange(number) {
return (number - 1) % 12 + 1;
}
var i;
for (i = 1; i <= 24; i++) console.log(i, toRange(i));
.as-console-wrapper { max-height: 100% !important; top: 0; }
answered Nov 8 at 11:27
Nina Scholz
168k1382145
168k1382145
add a comment |
add a comment |
up vote
0
down vote
maybe something like this:
function toRange(number) {
if (number > 12) {
if (number % 12 === 0)
return number - 12;
return number % 12
}
return number
}
add a comment |
up vote
0
down vote
maybe something like this:
function toRange(number) {
if (number > 12) {
if (number % 12 === 0)
return number - 12;
return number % 12
}
return number
}
add a comment |
up vote
0
down vote
up vote
0
down vote
maybe something like this:
function toRange(number) {
if (number > 12) {
if (number % 12 === 0)
return number - 12;
return number % 12
}
return number
}
maybe something like this:
function toRange(number) {
if (number > 12) {
if (number % 12 === 0)
return number - 12;
return number % 12
}
return number
}
answered Nov 8 at 11:28
simonecosci
56128
56128
add a comment |
add a comment |
up vote
0
down vote
Can also be thought of like - Get the remainder of number by 12 and return 12 in case remainder is 0.
function toRange(number) {
return number % 12 || 12;
}
for (var i = 1; i <= 24; i++) console.log(i, toRange(i));
add a comment |
up vote
0
down vote
Can also be thought of like - Get the remainder of number by 12 and return 12 in case remainder is 0.
function toRange(number) {
return number % 12 || 12;
}
for (var i = 1; i <= 24; i++) console.log(i, toRange(i));
add a comment |
up vote
0
down vote
up vote
0
down vote
Can also be thought of like - Get the remainder of number by 12 and return 12 in case remainder is 0.
function toRange(number) {
return number % 12 || 12;
}
for (var i = 1; i <= 24; i++) console.log(i, toRange(i));
Can also be thought of like - Get the remainder of number by 12 and return 12 in case remainder is 0.
function toRange(number) {
return number % 12 || 12;
}
for (var i = 1; i <= 24; i++) console.log(i, toRange(i));
function toRange(number) {
return number % 12 || 12;
}
for (var i = 1; i <= 24; i++) console.log(i, toRange(i));
function toRange(number) {
return number % 12 || 12;
}
for (var i = 1; i <= 24; i++) console.log(i, toRange(i));
answered Nov 8 at 12:21
Nitish Narang
1,40169
1,40169
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206762%2ffunction-that-given-a-number-returns-the-number-within-a-range%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password