searchInsertPosition

    0

    0

    YusifHasanov

    The code creates an object called searchInsert that will hold the largest number found after inserting a number into an array. The code loops through the numbers in the numbers array, looking for the biggest number. If the target number is bigger than the number in the i position, then the code increments the i position, increments the biggest number, and returns the i value.

    const searchInsert = (nums, target) => {
      let biggest = 0;
      for (let i = 0; i < nums.length; i++) {
        if (target > nums[i]) {
          biggest = i + 1;
        }
        if (target === nums[i]) {
          return i;
        }
      }
      return biggest;
    };
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.