JS顺序查找

function seqSearch(arr, data) {
    var returnIndex = -1
    var max = arr[0]
    var min = arr[0]
    arr.forEach((cur, index) => {
        if (cur === data) {
            returnIndex = index
        }
        if (cur > max) {
            max = cur
        }
        if (min > cur) {
            min = cur
        }
    })
    return {
        index: returnIndex,
        max: max,
        min: min
    }
}
var arr = [4, 5, 7, 1, 8, 19]
console.log(seqSearch(arr, 8))//{index: 4,max: 19,mix: 1}

你可能感兴趣的:(JS顺序查找)