I am trying to find the maximum of a number by swapping two digits. I have written the correct logic and it is getting to the part where I need to swap however after the swap i return the result and it isn't swapping?
var maximumSwap = function(num) {
let str = num.toString()
let left = 0
let right = 1
while (left !== str.length) {
if (parseInt(str[left]) < parseInt(str[right])) {
console.log(str[left], str[right])
let temp = str[left];
str[left] = str[right];
str[right] = temp;
return str
} else if (parseInt(str[left]) > parseInt(str[right])) {
if (left === right) {
left++
right = left + 1
} else if (left < right) {
right++
}
}
}
};
maximumSwap(2736)
// Input: 2736
// Output: 7236
// Explanation: Swap the number 2 and the number 7.