如何使用对象查找给定数字的范围

时间:2019-02-13 15:29:54

标签: javascript range

JSFiddle:https://jsfiddle.net/oaxgzsr1/

顺便说一句,随机数实际上不是随机数,而是变量。抱歉。

脚本:

  var random = 5;
  var rangeof = "";

  /* option #1 */
  if (random > 0 && random <= 5)
  rangeof = "1-5";
  if (random > 5 && random <= 10)
  rangeof = "6-10";
  if (random > 10 && random <= 15)
  rangeof = "10-15";
  if (random > 15 && random <= 20)
  rangeof = "16-20";
  if (random > 20 && random <= 25)
  rangeof = "21-25";
  //and so forth until we hit 100


  /* option #2 */
  var ranges = {
  1: "1-5",
  2: "1-5",
  3: "1-5",
  4: "1-5",
  5: "1-5",
  6: "6-10",
  7: "6-10",
  8: "6-10",
  9: "6-10",
  10: "6-10"
  //and so forth until we hit 100
  };

  alert(ranges[5]);

如您所见,它可能变得冗长且很难维护。

有什么办法可以做这样的事情?

  var ranges = {
    1-5: "1-5",
    6-10: "6-10",
    11-15: "11-15",
    16-20: "16-20"
  }

因此对于任何给定的数字,我都可以使用范围拉出键值。

例如:

如果数字为7,则它在6-10键范围内,而我得到的值为6-10

6 个答案:

答案 0 :(得分:3)

逻辑是否总是相同且单调的?然后您可以这样做:

const range = 5;

function inWhatRange(num) {
   const multiple = Math.floor( (num-1) / range);
   return `${multiple*range + 1}-${(multiple+1)*range}`;
}

console.log(inWhatRange(3));
console.log(inWhatRange(5));
console.log(inWhatRange(10));
console.log(inWhatRange(11));

答案 1 :(得分:2)

您可以计算一个随机数的间隔。

var random = Math.floor(Math.random() * 100 + 1),
    size = 5,
    slot = Math.floor((random - 1) / size),
    interval = [slot * size + 1, (slot + 1) * size].join('-');

console.log(random, interval);

答案 2 :(得分:2)

基本计算将找到值所属的存储区:

var x = 7;

var lower = Math.floor((x - 1) / 5) * 5;
var upper = lower + 5;
var range = (lower + 1) + "-" + upper; // "6-10"

答案 3 :(得分:2)

聚会晚一点,但是效果很好

   function getRange(x, interval, start, end) {
      if(x < interval) {
        console.log("range is " + start + " - " + interval);
      } else {
        var set = Math.ceil(x/interval);
        console.log("range is " + ((set * interval) - (interval - 1)) + " - " + set * interval)
      }
    }

    getRange(26, 5, 1, 100) // "range is 26 - 30"
    getRange(5, 5, 1, 100) // "range is 1 - 5"
    getRange(12, 5, 1, 100) // "range is 11 - 15"

答案 4 :(得分:1)

您可以使用一系列ifs:

let random = Math.round(Math.random() * 99 + 1) // I assume the number is never below 1
let rangeof


if (random < 6)
  rangeof = "1-5"
else if (random < 15)
  rangeof = "6-14"
else if (random < 100)
  rangeof = "15-99"
  
console.log(rangeof)

或者仅将边界存储在数组中(假设没有空间隔)并遍历它们:

let bounds = [1, 6, 15, 60, 100]
let random = Math.round(Math.random() * 99 + 1)
let rangeof


upper = bounds.findIndex(bound => bound > random)
if (upper == 0) throw 'bad number, should be a positive integer' 
if (upper == -1) throw `bad number, should be lower than ${bounds[bounds.length - 1]}`
rangeof = bounds[upper - 1] + '-' + (bounds[upper] - 1)

console.log(`${random} is in ${rangeof}`)

答案 5 :(得分:1)

您可以为此创建一个简单的函数。 只需按照特定的范围步长在您的范围内进行迭代,然后检查最小值/最大值。

const startRange = 1;
const endRange = 20;
const rangeStep = 4;

function getNumberRange(number){
  for(let i = startRange; i<endRange; i+=rangeStep+1){
    if(number >= i && number <= i+rangeStep){ 
      console.log(`${number} range: ${i}-${i+rangeStep}`);
    }
  }
}


getNumberRange(3);
getNumberRange(5);
getNumberRange(10);
getNumberRange(11);