How to create a binary search with recursion

I am attempting to write a “binary search” which I’ve never done before. The code below does not work when the value searched for is 6 or 2 and I want to know what I am doing wrong and how to remedy it. EDIT To explain what it is suppose to do (based on my understanding) a binary search requires that an array is already sorted, it then looks for the mid-point index of an array. For example, if an array had nine indexes (0-8)the the mid point would be index 4. The algorithm then determines if that mid point

How can i get the number without repetition?

i want to get the number with only one repitition: i want to get [1,2,1], but right now i am getting only [2] Answer Just overwrite your original array using Array.prototype.filter:

Build tree from edge pairs and root

I’m trying to write a program that takes an array of edge pairs and turns it into a tree. I’m given a root. In this example, the root is 2. The only constraint is, each node can max have 2 children. Sample input: Expected output: Would look something like this: This is my attempt so far: Feel like this should be pretty simple but I’m missing something.. Any help? 🙂 Answer Wow, I love this question. And is pretty challenging too! This was one of my first time to take a recursive approach to a certain problem. And I think

Improve speed of my binary search algorithm

I have written a binary search algorithm in JavaScript: I wanted to ask if I can improve this algorithm to search faster or if some mistake is made here? EDIT: thank you guys for your help, this solution should work correctly now: Answer You are taking values as indices. If you take greater values than indices, you see your codes does not work. Instead, you could take the index of middle for left or rightif not found.

How to reconstruct Json

I am currently having problem on modifying a Json schema, the schema is below: I know this can be done using recursion, and I have tried to fetch the required item(code is below), but I have no idea how to put the required item back into properties and change to boolean value. Thanks in advance. I want to put it into such format: Answer You can use recursive function with for…in loop and update object with required key on any level.