Implement the Binary Search Algorithm - Step 16

Tell us what’s happening:

The final step of this workshop asks me to change the return of my function to return both the pathToTarget variable and a template literal message Value found at index ${mid} yet the code does not pass stating: You should return pathToTarget, \Value found at index ${mid}’ inside the if’ statement.

I think this may be a bug?

Your code so far

function binarySearch(searchList, value) {
  let pathToTarget = [];
  let low = 0;
  let high = searchList.length - 1;
  while (low <= high) {
    let mid = Math.floor((low + high) / 2);
    let valueAtMiddle = searchList[mid];
    pathToTarget.push(valueAtMiddle);

    if (value === valueAtMiddle) {

// User Editable Region

      return pathToTarget, `Value found at index ${mid}`;

// User Editable Region


    } else if (value > valueAtMiddle) {
      low = mid + 1;
    } else {
      high = mid - 1;
    }
  }
  return [[], "Value not found"];

}

console.log(binarySearch([1, 2, 3, 4, 5], 3));
console.log(binarySearch([1, 2, 3, 4, 5, 9], 4));
console.log(binarySearch([1, 3, 5, 9, 14, 22], 10));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36

Challenge Information:

Implement the Binary Search Algorithm - Step 16

hello!

You can only return one thing at once. If you have to return multiple things, you have to wrap them in something. You already did something like this in the previous step -

Try to use this as a reference.

Thank you, your solution worked. It seems so obvious now…

Thanks, this worked!!