Update index.js

Updated swap method, for for to eliminate the multiple nested if statements and to format the whitespaces.