As an additional note, in addition to what has already been said, your source code could work with some minor changes: convert a string to an array of 1-character substrings (using split), process this array and convert it back to the string when you are done (with using association).
NB: the idea is to highlight the difference between accessing a character in a string (which cannot be changed) and processing an array of substrings (which can be changed). Fabricator's solution seems to be better.
var swapCase = function(str){
var letters = str.split("");
for(var i = 0; i<letters.length; i++){
if(letters[i] === letters[i].toLowerCase()){
letters[i] = letters[i].toUpperCase();
}else {
letters[i] = letters[i].toLowerCase();
}
}
str = letters.join("");
console.log(str);
}
var text = 'So, today we have REALLY good day';
swapCase(text);
source
share