Allegro75,
чуть покороче ...
const invertCase = (str) => {
let index = 0;
let result = '';
let lowerStr = str.toLowerCase();
let upperStr = str.toUpperCase();
for (; index < str.length; index++){
if (str[index] === lowerStr[index]) {
result += upperStr[index];
}
else result += lowerStr[index];
}
return result;
}
alert(invertCase('Hello, World!'));