Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return inside while loop should be useable #108

Open
kaidesu opened this issue Feb 12, 2022 · 0 comments
Open

return inside while loop should be useable #108

kaidesu opened this issue Feb 12, 2022 · 0 comments
Labels
change Change an existing feature

Comments

@kaidesu
Copy link
Member

kaidesu commented Feb 12, 2022

Currently, if using a return within a while loop, nothing happens. For example, the follow doesn't work when returning the pivot value directly:

function search(numbers, value) {
  left = 0
  right = numbers.length() - 1
  
  while (left <= right) {
    pivot = left + ((right - left) / 2).round()

    if (numbers[pivot] == target) {
      return pivot // this should exit the loop and return the value
    }

    if (value < numbers[pivot]) {
      right = pivot - 1
    } else {
      left = pivot + 1
    }
  }

  return -1
}

print(search([1, 2, 3], 2)) // should return 1
@kaidesu kaidesu self-assigned this Feb 12, 2022
@kaidesu kaidesu added the change Change an existing feature label Feb 12, 2022
@kaidesu kaidesu added this to the v1.0 Beta 2 milestone Feb 12, 2022
@kaidesu kaidesu removed their assignment Oct 27, 2023
@kaidesu kaidesu removed this from the v1.0 Beta 2 milestone Oct 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
change Change an existing feature
Projects
None yet
Development

No branches or pull requests

1 participant