0

The output of below code is '1536' but it should be '3000', but because of a "0" in front of '3000' for some reason it's treated as "1536", what might be the reason for this behavior?


// peak element
let arr = [1, 02, 03000, 4, 5, 6, 7, 8, 900, 100];

function peakElement(arr) {
    let maxElement = arr[0];
    // arr.forEach(element => {
    //     if(element>maxElement) maxElement = element
    // });
    for (let index = 0; index < arr.length; index++) {
        if (arr[index] > maxElement) maxElement = arr[index];
    }
    return maxElement;
}

console.log(peakElement(arr));

I tried vsCode and even directly run the script directly in console in chrome

0 Answers0