Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update 0034-find-first-and-last-position-of-element-in-sorted-array.js
Making variable names more descriptive.
  • Loading branch information
aadil42 authored Jul 3, 2023
commit b44ad1666937ca3a6e1acc53c46c4dd8fd109d47
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var searchRange = function(nums, target) {
result.push(binarySearch(true));
result.push(binarySearch(false));

function binarySearch(leftBias) {
function binarySearch(isLeftBias) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid nested functions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

let left = 0;
let right = nums.length - 1;
let index = -1;
Expand All @@ -28,9 +28,10 @@ var searchRange = function(nums, target) {
if(target < nums[mid]) {
right = mid-1;
}
// this is the meat of the code
if(target === nums[mid]) {
if(leftBias) {

const isTarget = target === nums[mid];
if(isTarget) {
if(isLeftBias) {
index = mid;
right = mid - 1;
} else {
Expand Down