mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00

* FindMinIterator Do the `standard` thing. Rename `FindMin` to `FindMinIterator` Rename to `FindMinIterator` Pull `FindMin` from `master` * Remove these separator comments. Co-authored-by: RuSaG0 <mirzoev-ruslan-2000@mail.ru>
37 lines
850 B
JavaScript
37 lines
850 B
JavaScript
/**
|
|
* @function FindMinIterator
|
|
* @description Function to find the minimum number given in an array.
|
|
*/
|
|
|
|
const FindMinIterator = (_iterable, _selector = undefined) => {
|
|
let min
|
|
|
|
const iterator = _iterable[Symbol.iterator]()
|
|
if (!_selector) {
|
|
let current = iterator.next()
|
|
if (current.done) { return undefined }
|
|
min = current.value
|
|
|
|
current = iterator.next()
|
|
while (!current.done) {
|
|
const x = current.value
|
|
if (x < min) { min = x }
|
|
current = iterator.next()
|
|
}
|
|
} else {
|
|
let current = iterator.next()
|
|
if (current.done) { return undefined }
|
|
min = _selector(current.value)
|
|
|
|
current = iterator.next()
|
|
while (!current.done) {
|
|
const x = _selector(current.value)
|
|
if (x < min) { min = x }
|
|
current = iterator.next()
|
|
}
|
|
}
|
|
return min
|
|
}
|
|
|
|
export { FindMinIterator }
|