JSFiddle - React, Tailwind, and code Playground

by Anurag Udasi

JavaScript

var a = [3, 4, 5, 6, 7, 8, 9], j = a.length, i = 0, isIOdd = true, idJOdd = true, temp;
while(i < j) {
    // if i is odd and if j is even, do not do anything
    // if i is odd and if j is odd, then i++
    // if i is even and if j is odd, then swap
    // if i is even and j is even, then j--
    isIOdd = (a[i] % 2) == 1 ? true : false;
    isJOdd = (a[j] % 2) == 1 ? true : false;
    if(isIOdd && !isJOdd) {
        i++;
        j--;
    }
    else if(isIOdd && isJOdd) {
        i++;
    }
    else if(!isIOdd && isJOdd) {
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
    else {
        j--;
    }
}

alert(a);