JS solution using string and array manipulation


#1

module.exports = {
//param A : integer
//return an integer
reverse : function(A){
const asBitStringArray = this.toBitArray(A);
let result = asBitStringArray;
for (let i = 0; i < 16; i++) {
result = this.swap(result, i, 32-1-i);
// console.log(result);
}
return this.toNumber(result);
},

toBitArray: function(num) {
    return num.toString(2).padStart(32, "0");
},

toNumber: function(str) {
    return Number("0b" + str);
},

swap: function(str, i, j) {
    const asArray = str.split("");
    const atI = asArray[i];
    asArray[i] = asArray[j];
    asArray[j] = atI;
    return asArray.join("");
}

};