簡體   English   中英

if語句僅在for循環的索引2上執行

[英]if statement only executing on index 2 of for loop

我的coffeescript中有這個for循環:

compareVersions = (current, minimum) ->
    console.log('current ', current)
    console.log('minimum ', minimum)
    current_parts = current.split '.'
    minimum_parts = minimum.split '.'
    for partIndex in [0..Math.min(current_parts.length, minimum_parts.length)]
       console.log('partIndex ', partIndex)
       if (+current_parts[partIndex] || 0) < (+minimum_parts[partIndex] || 0)
         console.log('current_parts.length1 ', current_parts[partIndex])
         console.log('minimum_parts.length1 ', minimum_parts[partIndex])
         return false
    console.log('PC current_parts.length2 ', current_parts[partIndex])
    console.log('PC minimum_parts.length2 ', minimum_parts[partIndex])
    true

它旨在比較軟件版本,如果當前版本低於最低版本,則返回false。

我添加了console.log來顯示正在發生的事情。

這是我得到的輸出。 在這種情況下,當前版本比最小版本高,但是if語句僅在索引2上執行,在這種情況下,當前版本的索引號比最小版本小。

current  3.4.1.35
minimum  3.3.3
partIndex  0
partIndex  1
partIndex  2
current_parts.length1  1
minimum_parts.length1  3

if語句應針對for循環的每次迭代執行。 這里有什么問題阻止了這種情況?

if語句在for循環的每次迭代中執行,但是當if語句為true時,將打印current_parts.length1和minimum_parts.length1。

您的if語句的執行為:

3 < 3 false
4 < 3 false
1 < 3 true
    print current_parts.length1
    print minimum_parts.length1

如果要顯示current_parts.length1和minimum_parts.length1,請將它們放在if語句之外,以便在執行if語句時可以看到這些值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM