LeetCode exercise issue

Hi, working on LeetCode problem 278 First bad version.

Have this code:

var solution = function(isBadVersion) {
    return function(n) {
        let begin = 1;
        let ending = n;
        while (begin < ending){
            let middleVal = Math.floor((ending + begin) / 2);
            if(isBadVersion(middleVal)){
                ending = mid
            } else {
                if(start === ending) return ending;
                start = middleVal
            }
        }
        return ending;
    };
};

it seems to work fine in the LeetCode console. But I want to make it work in PythonTutor:

I am calling it with this code: console.log(solution(3))
but it does not work. It returns nothing. Am I calling it wrong? Thanks

I tried to run your code snippet in the console of Leetcode. Unfortunately, it didn’t work. I found several variables in your code are undefined. For example: “start”. So I refactored your code and passed the test case in Leetcode. I post my code snippet as follows and I hope that will help:

var solution = function(isBadVersion) {
return function(n) {
let begin = 1;
let end = n;
let pos = 1;

    while (begin <= end){
        let middleVal = Math.floor(begin + (end - begin) / 2);
        if(isBadVersion(middleVal)){
            pos = middleVal;
            end = middleVal - 1;
        } else {
           begin = middleVal + 1; 
        }
    }
    return pos;
};

};

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.