Skip to main content
Post Undeleted by peterSO
deleted 28 characters in body
Source Link
peterSO
  • 3.5k
  • 13
  • 14

Here is a revised, simpler version of your code.

function indexFirstLast(target, arr) {
    let [first, last] = [-1, -1];
    for (let i = 0; i < arr.length - 1;&& iarr[i] >=<= 0;target; i--i++) {
        if (arr[i] === target) {
            last = i;
        if (first === -1) break;{
        }
    }
    for (let ifirst = 0; i <= last; i++) {i;
        if (arr[i] === target) {}
            firstlast = i;
            break;
        }
    }
    return [first, last];
}
function indexFirstLast(target, arr) {
    let [first, last] = [-1, -1];
    for (let i = arr.length - 1; i >= 0; i--) {
        if (arr[i] === target) {
            last = i;
            break;
        }
    }
    for (let i = 0; i <= last; i++) {
        if (arr[i] === target) {
            first = i;
            break;
        }
    }
    return [first, last];
}

Here is a revised, simpler version of your code.

function indexFirstLast(target, arr) {
    let [first, last] = [-1, -1];
    for (let i = 0; i < arr.length && arr[i] <= target; i++) {
        if (arr[i] === target) {
            if (first === -1) {
                first = i;
            }
            last = i;
        }
    }
    return [first, last];
}
deleted 31 characters in body
Source Link
peterSO
  • 3.5k
  • 13
  • 14

A simple, readable version.

function indexFirstLast(target, arr) {
    let [first, last] = [-1, -1];
    for (let i = arr.length - 1; i >= 0; i--) {
        if (arr[i] === target) {
            last = i;
            break;
        }
    }
    for (let i = 0; i <= last; i++) {
        if (arr[i] === target) {
            first = i;
            break;
        }
    }
    return [first, last];
}

A simple, readable version.

function indexFirstLast(target, arr) {
    let [first, last] = [-1, -1];
    for (let i = arr.length - 1; i >= 0; i--) {
        if (arr[i] === target) {
            last = i;
            break;
        }
    }
    for (let i = 0; i <= last; i++) {
        if (arr[i] === target) {
            first = i;
            break;
        }
    }
    return [first, last];
}
function indexFirstLast(target, arr) {
    let [first, last] = [-1, -1];
    for (let i = arr.length - 1; i >= 0; i--) {
        if (arr[i] === target) {
            last = i;
            break;
        }
    }
    for (let i = 0; i <= last; i++) {
        if (arr[i] === target) {
            first = i;
            break;
        }
    }
    return [first, last];
}
Post Deleted by peterSO
deleted 2 characters in body
Source Link
peterSO
  • 3.5k
  • 13
  • 14

A simple, readable version.

function indexFirstLast(target, arr) {
    let [first, last] = [-1, -1];
    for (let i = arr.length - 1; i >= 0; i--) {
        if (arr[i] === target) {
            last = i;
            breakbreak;
        }
    }
    for (let i = 0; i <= last; i++) {
        if (arr[i] === target) {
            first = i;
            breakbreak;
        }
    }
    return [first, last];
}

A simple, readable version.

function indexFirstLast(target, arr) {
    let [first, last] = [-1, -1];
    for (let i = arr.length - 1; i >= 0; i--) {
        if (arr[i] === target) {
            last = i;
            break
        }
    }
    for (let i = 0; i <= last; i++) {
        if (arr[i] === target) {
            first = i;
            break
        }
    }
    return [first, last];
}

A simple, readable version.

function indexFirstLast(target, arr) {
    let [first, last] = [-1, -1];
    for (let i = arr.length - 1; i >= 0; i--) {
        if (arr[i] === target) {
            last = i;
            break;
        }
    }
    for (let i = 0; i <= last; i++) {
        if (arr[i] === target) {
            first = i;
            break;
        }
    }
    return [first, last];
}
Source Link
peterSO
  • 3.5k
  • 13
  • 14
Loading