【问题标题】:positive/negative Modulo to get items in range正/负模数以获取范围内的项目
【发布时间】:2018-08-22 08:02:27
【问题描述】:

我查看了https://math.stackexchange.com/questions/519845/modulo-of-a-negative-numberModulo operation with negative numbers,但我仍然想知道如何使用负模值来获取范围内的项目。

这是我下面的问题之前的一个简化用例,我有一个包含 3 张幻灯片的幻灯片 (slidesCount = 3):

slide indexes:    0     1     2

现在我想从这些数字访问右边的幻灯片:

    -2   -1       0   1   2       3   4

           should match slide:

     1    2       0   1   2       0   1

所以我用index % slidesCount 介绍了这些案例:

                  0   1   2       3   4 

但不是负值。 -1 % 3 返回-1 那么slidesCount + index % slidesCount 如果索引为负数是正确的表达式吗?

首先,有没有更简单/更聪明的写法:

index = index % slidesCount + (index < 0 ? slidesCount : 0)

现在我的问题是每张幻灯片显示 3 个可见项目的幻灯片, 最后一张幻灯片可能只有一个项目(下面的索引 9),所以从这些数字:

  -3 -2 -1         0 1 2   3 4 5   6 7 8   9       10 11 12

我要匹配幻灯片:

      9              0       3       6     9           0

我希望下图有意义!请帮助我用最少的ifs 得出正确的方程式:

     -3 -2 -1               0 1 2   3 4 5   6 7 8   9             10 11 12
    |________|             |______________________________________________|
        ||                              ||                           ||
        ||                              Math.floor( i / visibleSlides )
Math.ceil(i / visibleSlides)            ||                           ||
        ||                              ||                           ||
        \/                              \/                           \/

        -1                    0       1       2     3                 4
       |___|                 |_______________________|              |___|
        ||                              ||                           ||
slidesCnt + i % visibleSlides       i % visibleSlides                || ??
        ||                              ||                           ||
        \/                              \/                           \/

         3                    0       1       2     3                 0

                                        || i * visibleSlides
                                        \/ 

         9                    0       3       6     9                 0

【问题讨论】:

  • items par slide的情况需要另外的控制结构,可以考虑拆分到另一个问题吗?
  • 你可以做index = (index + slidesCount) % slidesCount
  • 好主意。
  • 我在帖子中说的主要问题是关于每张幻灯片的项目,我不能把它分成另一个问题

标签: javascript modulo


【解决方案1】:

最后,这就是我解决问题的方法:

// Prevents out of range.
// e.g. -1, -2, 12, 13
let newIndex = (index + slidesCount) % slidesCount

// Calculates how many items are missing on last slide. 
// e.g. if 10 slides with 3 visible slides, 2 missing slides on last slide.
let lastSlideItems = slidesCount % visibleSlides || visibleSlides
let missingItems = visibleSlides - lastSlideItems

// If index is negative adjust by adding the missing slides.
newIndex += index < 0 ? missingItems : 0

// Always get the first item of the series of visible items.
// e.g. if index is 8 and 3 visible slides, newIndex will be 6. 
newIndex = Math.floor(newIndex / visibleSlides) * visibleSlides

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-04
    • 2017-02-20
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多