我想知道是否有人能向我解釋以下解決方案:
function smallestInt(arr) {
if (arr.length == 1) return arr[0] 。
return (Math. min(arr[0],smallestInt(arr.slice(1))));
}
console. log(smallestInt([42, 12, 8, 60, 12, 33, 21] )。 /> 8
特別是,堆疊會是什么樣子。如果有任何意見,我們將不勝感激。
uj5u.com熱心網友回復:
使用console.log是快速除錯的好辦法。它很容易得到,而且是你盒子里的一個偉大的工具,但它不是唯一的工具。在學習如何使用除錯器方面也有很多收獲。
設定除錯會話
Sources標簽運行除錯會話
除錯器現在會在每次達到L2時停止。請看下面的截屏。注意除錯器是如何在右上角顯示arr的當前狀態的:
你可以做的另一件事是 "回到過去"。請注意,我們可以來回檢查堆疊中的每一步。(見右下方。)
除非它是一個練習,否則你不需要遞回。這里有幾個備選方案:
Math.min( ... [42, 12, 8, 60。12, 33, 21] )。)
//=> 8
或者用reduce:
[42, 12, 8, 60, 12, 33, 21】。] reduce((n, x) =>/span> Math. min(n, x), Infinity)
//=> 8。
uj5u.com熱心網友回復:
這只是簡單地比較最后一個和倒數第二個索引,并在每一步中選擇較小的索引。
。// You can print it with slightly rearrangement of the code
function smallestInt(arr) {
if (arr.length == 1) return arr[0] 。
let last = smallestInt(arr.slice(1)
let res = (Math.min(arr[0],last))。
console.log(arr[0], last, res) 。
returnres。
}
console. log(smallestInt([42, 12, 8, 60, 12, 33, 21] )。
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
下面的步驟可以作為日志輸出
let stage = 0;
function smallestInt(arr) {
console.log(`arr = [${arr}]`) 。
if (arr.length == 1) return arr[0] 。
let temp, newArr;
const min = Math. min(arr[0], temp = smallestInt(newArr = arr. slice(1) )。
console.log(`stage: ${stage } => Math.min(arr[0] = ${arr[0] }, smallestInt( arr. slice(1)) = [${newArr}] = ${min}`)
return min。
}
console. log(smallestInt([42, 12, 8, 16] )。
/* This is not a part of answer. 它只是為了給輸出填充高度。所以請忽略它 */
.as-console-wrapper { max-height: 100% ! important; top: 0; }
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
讓我們一步一步地進行處理吧
1)當函式開始時arr = [42,12,8,16]
注意:我們知道arr.length不等于1所以它不會回傳arr[0]
而我們要回傳的結果值是
。Math。 min(arr[0],smallestInt(arr.slice(1) )
這可以解釋為:
Math。 min(arr[0] = 42, smallestInt( arr. slice(1)) = [12,8, 16] //這里的遞回 //這里的遞回
2)當函式開始時
arr = [12, 8, 16]
注意:我們知道arr.length不等于1所以它不會回傳arr[0]
而我們要回傳的結果值是
。Math。 min(arr[0],smallestInt(arr.slice(1) )
這可以解釋為:
Math。 min(arr[0] = 12, smallestInt( arr. slice(1)) = [8,16] //遞回此處
3)當函式開始時
arr = [8, 16]
注意:我們知道arr.length不等于1所以它不會回傳arr[0]
而我們要回傳的結果值是
。Math。 min(arr[0],smallestInt(arr.slice(1) )
這可以解釋為:
Math。 min(arr[0] = 8, smallestInt( arr. slice(1)) = [16] //遞回此處
4)當函式開始時
arr = [16]
注意:arr.length等于1,所以它將回傳arr[0]即16
3)現在從
smallestInt( newArr = arr.slice( 1 ))回傳的值是16所以函式的回傳值將是
Math。 min(8, 16) ==> 8將是回傳值的整個功能的。
2)現在從
smallestInt( newArr = arr.slice( 1 ))回傳的值是8所以函式的回傳值將是
Math。 min(12, 8) ==> 8將是回傳值的整個功能的。
1)現在從smallestInt( newArr = arr.slice( 1 ))回傳的值是8所以函式的回傳值將是
Math。 min(42, 8) ==> 8將是回傳值的整個函式的回傳值。
所以console.log( smallestInt( [42, 12, 8, 16] ) ;的最終值將是8
uj5u.com熱心網友回復:
讓我們拿小陣列 [42, 12, 8, 60]
因此,它開始尋找42和再次呼叫smallestInt([12, 8, 60])/code>回傳的結果之間的最小值。在這里,我們呼叫smallestInt時從我們的陣列中洗掉了第一個索引元素。
如果我們把這個呼叫堆疊擴展出來,它將會是這樣的:
smallestInt([42, 12, 8, 60]) = Math. min(42, smallestInt([12, 8, 60]) 。)
現在我們之前的smallestInt呼叫將有超過1個長度的陣列,所以它確實做了進一步的遞回呼叫,通過設定第一個元素作為Math.min的第一個引數和第二個引數作為呼叫smallestInt的陣列洗掉第一個索引元素,即12。因此,我們的呼叫堆疊將看起來像這樣。
smallestInt([42, 12, 8, 60]) = Math. min(42, Math. min(12, smallestInt([8, 60])。
再如前述:
smallestInt([42, 12, 8, 60]) = Math. min(42, Math. min(12, Math. min(8, smallestInt([60]))));
因為現在我們的smallestInt([60])得到1個長度的陣列[60]作為輸入;if (arr.length ==1)條件得到滿足,即smallestInt將回傳60。然后我們就脫離了遞回呼叫。
現在它開始反向遍歷,即它將開始尋找最小值。
smallestInt([42, 12, 8, 60]) = Math. min(42, Math. min(12, Math. min(8, 60) )。
smallestInt([42, 12, 8, 60]) = Math. min(42, Math. min(12, 8)。
smallestInt([42, 12, 8, 60]) = Math. min(42, 8)。
smallestInt([42, 12, 8, 60]) = 8。
最后回傳8;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/316538.html
標籤:



