String.prototype.replaceAt=function(index, ch) {
return this.substr(0, index) + ch + this.substr(index + ch.length);
}
function swap(s, index1, index2) {
var tmp = s[index1];
s = s.replaceAt(index1, s[index2]);
s = s.replaceAt(index2, tmp);
return s;
}
s = "abcdef";
for (var i = 0; i < s.length; i++) {
if (i % 2 === 0) {
if ((i + 1) < s.length) {
s = swap(s, i, i + 1);
}
}
}
document.write(s);
/*
run:
badcfe
*/