Preview:
'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
    inputString += inputStdin;
});

process.stdin.on('end', function() {
    inputString = inputString.split('\n');

    main();
});

function readLine() {
    return inputString[currentLine++];
}


function main() {
    const n = parseInt(readLine().trim(), 10);

    const a = readLine().replace(/\s+$/g, '').split(' ').map(aTemp => parseInt(aTemp, 10));

    var numberOfSwaps = 0;
    for (var i = 0; i < n; i++) {
    
    var item;
    for (var j = 0; j < n - 1; j++) {
    
        if (a[j] > a[j + 1]) {
            item = a[j];
            a[j]= a[j + 1];
            a[j + 1] = item;
            numberOfSwaps++;
        }
    }
   
   
    if (numberOfSwaps == 0) {
        break;
    }
   
}
    
   console.log("Array is sorted in", numberOfSwaps,"swaps.");
   console.log("First Element:", a[0]);
   console.log("Last Element:", a[a.length-1]);
    
}
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