function exam04(array){
//declare variables
let countSwap = 0;
let n = array.length;
for (let i = 1; i < n; i++) {
// extract the elements
let current = array[i];
let j = i-1;
while ((j > -1) && (current < array[j])) {
array[j+1] = array[j];
j--;
countSwap++; // count swap
}
array[j+1] = current;
}
return countSwap;
}
function test_01() {
if (exam04([2, 1, 3, 1, 2]) === 4) {
console.log("pass");
} else {
console.log("fail");
}
}
test_01();