'Search Insert Position-Wrong answe

I'm solving a problem (leetcode 35). My code was accepted in run code result but when I submit it returns wrong answer.I don't really understand what is wrong in my answer .

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

Example 1:

Input: [1,3,5,6], 5
Output: 2
Example 2:

Input: [1,3,5,6], 2
Output: 1
Example 3:

Input: [1,3,5,6], 7
Output: 4
Example 4:

Input: [1,3,5,6], 0
Output: 0

below is my code.Please help me know where the bug is.Thanks in advance

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var searchInsert = function(nums, target) {
 var min = 0;
 var max = nums.length - 1;
    var guess;
    
    while(min <= max) {
    guess = Math.floor((max + min) / 2);
        if (nums[guess] === target) {
        return guess;
    }
    else if (nums[guess] < target) {
        min = guess + 1;
    }
    else {
        max = guess - 1;
    }
    }
};
    

submission details


Input: [1,3,5,6]
2
Output: undefined
Expected: 1


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source