Case change to javascript

I created a script that changes the case, but the result of using it in the text is exactly the same text without a single change. Can someone explain this?

var swapCase = function(letters){
    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();
        }
    }
   console.log(letters);
}

var text = 'So, today we have REALLY good day';

swapCase(text);
+7
source share
6 answers

As Yang said, you need to create a new line.

var swapCase = function(letters){
    var newLetters = "";
    for(var i = 0; i<letters.length; i++){
        if(letters[i] === letters[i].toLowerCase()){
            newLetters += letters[i].toUpperCase();
        }else {
            newLetters += letters[i].toLowerCase();
        }
    }
    console.log(newLetters);
    return newLetters;
}

var text = 'So, today we have REALLY good day';

var swappedText = swapCase(text); // "sO, TODAY WE HAVE really GOOD DAY"
+12
source

You can use this simple solution.

var text = 'So, today we have REALLY good day';

var ans = text.split('').map(function(c){
  return c === c.toUpperCase()
  ? c.toLowerCase()
  : c.toUpperCase()
}).join('')

console.log(ans)
Run codeHide result

Using ES6

var text = 'So, today we have REALLY good day';

var ans = text.split('')
.map((c) =>
 c === c.toUpperCase() 
 ? c.toLowerCase()
 : c.toUpperCase()
).join('')

console.log(ans)
Run codeHide result
+3
source

, . char , .

function swapCase(letters) {
  return letters.replace( /\w/g, function(c) {
    if (c === c.toLowerCase()) {
      return c.toUpperCase();
    } else {
      return c.toLowerCase();
    }
  });
}
0

! :

string.replace(/\w{1}/g, function(val){
    return val === val.toLowerCase() ? val.toUpperCase() : val.toLowerCase();
});
0

, XOR ^.
, , toUppserCase/toLowerCase

"So, today we have REALLY good day"
.split("")
.map((x) => /[A-z]/.test(x) ? String.fromCharCode(x.charCodeAt(0)  ^ 32) : x)
.join("")


, , map , .
map RegEx , : /[Az]/.test(x) , XOR ^ . , . charCodeAt char UTF-16. XOR (^) . String.fromCharCode . RegEx false ( ABC), .

:

0

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);
-1
source

Source: https://habr.com/ru/post/1546444/


All Articles