8

I need help on this: cant get it to work.

 3 years ago
source link: https://dev.to/millidavitti/i-need-help-on-this-cant-get-it-to-work-3nke
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

I need help on this: cant get it to work.

Aug 26

・1 min read

// Recursive Binary Search
function recursive(list, target) {
if (list.length === 0) {
return false;
} else {
let midpoint = Math.floor(list.length / 2);
if (list[midpoint] === target) {
return true;
} else {
if (list[midpoint] < target) {
return recursive(list[midpoint + 1], target);
} else {
if (list[midpoint] > target) return recursive(list[midpoint], target);
}
}
}
}

function verifyRecursive(result) {
return Target found : ${result};
}

Discussion (3)

pic

CollapseExpand

Probably that :

function binarySearch(list, target) {
    if (list.length === 0) {
        return false;
    }

    let midpoint = Math.floor(list.length / 2);
    if (list[midpoint] === target) {
        return true;
    }

    const left = list.slice(midpoint -1);
    const right = list.slice(midpoint +1, list.length);

    return binarySearch(right, target) || binarySearch(left, target);
}
Enter fullscreen modeExit fullscreen mode

Comment button Reply

CollapseExpand

We can't help you like this, please take some time and read the editor guide, and try to compose the question a bit better and you will be more likely to be helped. You can't expect us to do all the job right? ;D

Comment button Reply

CollapseExpand

This is not a good way to ask for help. Simply saying "this does not work" tells us nothing.

  • What is the desired output?
  • What is the actual output?
  • Are there any error messages?

Comment button Reply


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK