sortedIndex

    0

    2

    Jannick Holm

    The sortedIndex function uses the built-in Array.findIndex function to get an index that indicates the location of the element that is the lowest value in the array. If the value at that location is lower than the value of the element at the first position in the original array, the index is negative one, indicating that the value at that location should be moved down one position in the array. If the value at the location is higher than the value of the element at the first position in the original array, the index is positive one, indicating that the value at that location should be moved up one position in the array.

    const sortedIndex = (arr, n) => {
      const isDescending = arr[0] > arr[arr.length - 1];
      const index = arr.findIndex(el => (isDescending ? n >= el : n <= el));
      return index === -1 ? arr.length : index;
    };
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    Legal
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.