function swapToGetMax(number) {
let numStr = number.toString();
let max = -1;
for (let i = 0; i < numStr.length - 1; i++) {
for (let j = i + 1; j < numStr.length; j++) {
let swapped = numStr.slice(0, i) + numStr[j] + numStr.slice(i + 1, j) + numStr[i] + numStr.slice(j + 1);
let swappedNumber = parseInt(swapped);
if (swappedNumber > max) {
max = swappedNumber;
}
}
}
console.log(`原始数: ${number}`);
console.log(`交换后获得的最大数: ${max}`);
return max;
}
console.log(swapToGetMax(12348963));
function colorHexToRgb(hexColor) {
if (!hexColor || typeof hexColor !== 'string') {
return hexColor;
}
const match = hexColor.toLowerCase().match(/^#([0-9a-f]{3}|[0-9a-f]{6})$/);
if (!match) {
return hexColor;
}
const hexStr = match[1];
let rStr, gStr, bStr;
if (hexStr.length === 3) {
rStr = hexStr.slice(0, 1) + hexStr.slice(0, 1)
gStr = hexStr.slice(1, 2) + hexStr.slice(1, 2)
bStr = hexStr.slice(2) + hexStr.slice(2)
} else {
rStr = hexStr.slice(0, 2)
gStr = hexStr.slice(2, 4)
bStr = hexStr.slice(4)
}
const rgb = [rStr, gStr, bStr].map(hexToDecimal).join()
return `rgb(${rgb})`
}
function hexToDecimal(hex) {
const hexTotalStr = "0123456789abcdef";
let total = 0;
const decicalStr = hex.split('').reverse().forEach((str, index) => {
total += hexTotalStr.indexOf(str) * Math.pow(16, index)
})
return total;
}
console.log(colorHexToRgb("#FF0001"))
console.log(colorHexToRgb("#abc"))
console.log(colorHexToRgb("#002233"))