Function that given a number returns the number within a range

Hommer Smith

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.

Nina Scholz

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; }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Show multiples of a given number within a range

Random number within range and a given granularity in Golang

How can I wrap a given number within a given [range)?

Find what percentile a number ranks within a given range?

How to get a random multiple of a given number within a range

Number of days of current week within a given date range

Scale a number by given range

Number of combinations given range

Function to split text evenly within the given number in javascript

Is there a function to retrieve the number of available distinct values within a range?

Math function to find the biggest multiple of a number within a range

How can I distribute a pool to a given number of items within a given range

How to create a function that returns the number of nodes in a tree on a given level

Function that returns the number of "superiors" for each element in a given list

How to find a range of given number?

Kaprekar Number between given range

Convert an increasing number to a decreasing number within a range

Detecting if a Number is within a Certain Range

Check which range a number is within

Dataset Locate number within range

Excel Find Number Within a Range

LISP function which, given a number and a list, returns the first even number greater than n

Python - function returns true when a given number is a prime number or else false

Function that, given a csv file path and a keyword, returns the line number of the keyword and the line number of the next blank line

In a Java unit test, how do I assert a number is within a given range?

How to check if a float value is within a certain range and has a given number of decimal digits?

Is there any way to generate a random number within a given range using only CSS?

Random number generator doesn't produce expected numbers within a given range using values from input fields

Write a function that removes any properties on the given object whose values are strings longer than the given number and returns the object