/*You have an array of non-negative integers. You need to calculate the difference between the 1st biggest number and the 2nd biggest number of the array.*/ function diffBig2(arr) { let maxNum = Math.max(arr[0], arr[1]); let secondMax = Math.min(arr[0], arr[1]); for (let j = 1,i = 2; i < arr.length; ++i, ++j){ if (arr[i] > maxNum) { secondMax = maxNum; maxNum = arr[i]; } else if (arr[i] > secondMax) { secondMax = arr[i]; } } return maxNum - secondMax; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter