findLastKeyWithVal

findLastKeyWithVal

  • (value: any) => (sequence: Sequence) => number | undefined
  1. Find the last index which has value.

    const findLastKeyOfA = findLastKeyWithVal('a')
    
    findLastKeyOfA(['a', 'b', 'a']) // is 2
    findLastKeyOfA(['b', 'c']) // is undefined
    
    findLastKeyOfA('aba') // is 2
    const findLastKeyOfA = findLastKeyWithVal('a')
    
    findLastKeyOfA(['a', 'b', 'a']) // is 2
    findLastKeyOfA(['b', 'c']) // is undefined
    
    findLastKeyOfA('aba') // is 2
    
    const findLastKeyOfA = findLastKeyWithVal('a')
    
    findLastKeyOfA(['a', 'b', 'a']) // is 2
    findLastKeyOfA(['b', 'c']) // is undefined
    
    findLastKeyOfA('aba') // is 2
    const findLastKeyOfA = findLastKeyWithVal('a')
    
    findLastKeyOfA(['a', 'b', 'a']) // is 2
    findLastKeyOfA(['b', 'c']) // is undefined
    
    findLastKeyOfA('aba') // is 2
    
  2. Sometimes we want to find the last index to a value. Below, Chris starts a weightlifting program by seeing how many reps he can lift per level. Let's find the highest level where he can only lift one rep.

    const repsPerWeightLevel = [12, 12, 10, 8, 6, 3, 1, 1, 0]
    
    const findOneRepMax = findLastKeyWithVal(1)
    const maxWeightLevel = findOneRepMax(repsPerWeightLevel)
    console.log(maxWeightLevel)
    // is 7
    const repsPerWeightLevel = [12, 12, 10, 8, 6, 3, 1, 1, 0]
    
    const findOneRepMax = findLastKeyWithVal(1)
    const maxWeightLevel = findOneRepMax(repsPerWeightLevel)
    console.log(maxWeightLevel)
    // is 7
    
    const repsPerWeightLevel = [
      12, 12, 10, 8, 6, 3, 1, 1, 0,
    ]
    
    const findOneRepMax = findLastKeyWithVal(1)
    const maxWeightLevel = findOneRepMax(
      repsPerWeightLevel
    )
    console.log(maxWeightLevel)
    // is 7
    const repsPerWeightLevel = [
      12, 12, 10, 8, 6, 3, 1, 1, 0,
    ]
    
    const findOneRepMax = findLastKeyWithVal(1)
    const maxWeightLevel = findOneRepMax(
      repsPerWeightLevel
    )
    console.log(maxWeightLevel)
    // is 7