javascript - 带有循环javascript的数字范围

标签 javascript angularjs coffeescript

我需要设置一个从 1 到 15 的范围以及某种反向。 目前我使用下一个脚本:

$scope.range = (min, max) ->
  input = []
  i = min
  while i < max
    input.push i
    i += 1
  input

所以如果输入range(1,15),它将是1 2 3 4 5 6 7 8 9 10 11 ... 15

我需要的是,当 range(10, 3) 时,它应该放置 10 11 12 13 14 15 1 2 3

最佳答案

range = (min, max) ->
    input = []
    i = max
    while i > min
      input.push i
      i -= 1
    input

console.log(range( 10, 50 ));

这真的很简单,尝试设置 i = max 然后对 i 进行倒数,条件是 i 必须大于 min。就是这样。尝试上面的代码。

关于javascript - 带有循环javascript的数字范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32743023/

相关文章:

javascript - 阻止 jQuery 自动完成插件在用户单击返回时忘记文本

javascript - 即使我将范围选项设置为 false,为什么从指令内对外部范围的更改需要scope.$apply()?

javascript - 似乎无法访问 json 数据的特定元素

coffeescript - 这个语句可以写成一行吗?

Javascript Object.defineProperty 设置属性更改时触发的方法

javascript - 处理许多 DOM 更改

javascript - 基于时间的动态CSS的JS

javascript - 延迟加载不显示我的图像

javascript - 我想通过检查 NODE_ENV 在 header-view.js 中添加 GA 代码

javascript - $httpParamSerializer 的未知提供者