简体   繁体   中英

JavaScript: Can someone tell me what's going wrong with my code?

I've been doing JavaScript exercises since i haven't been doing this for too long. Got most of them working but i've been staring blind at this one.

I need to make all lowercase upper & vice versa. I checked the solution and it was helpfull but i'm sure i can get my own code ( which is very different from the answer ) working as well.

Thanks for all bits of help.

function swapCase (str) {
  var sen = str;
  for ( var i = 0; i < sen.length; i++) {
    if (sen.charAt(i) === sen.charAt(i).toLowerCase()) {
      sen.charAt(i).toUpperCase();
    } else if (sen.charAt(i) === sen.charAt(i).toUpperCase()) {
      sen.charAt(i).toLowerCase();
    }
  } return sen;
}

console.log(swapCase("UPlowUPlow"));

Ps: I am aware that it's not the cleanest bit of code but i've been working on it for a while. :)

toUpperCase and toLowerCase return the modified string, they don't modify it in place. So you'd have to put that value into the string you're processing, eg:

sen = sen.substring(0, i - 1) + sen.charAt(i).toUpperCase() + sen.substring(i + 1);

As that's fairly awkward, you'd probably be better off converting the string into an array of single-character strings, processing the array, then combining it again.

You can convert a string into an array of single character strings like this:

theArray = theString.split("");

...then process each entry, in a loop or via Array#map :

theArray[i] = theArray[i].toUpperCase();

...and then convert it back when done:

theString = theArray.join("");

Here's an example using Array#map :

 function swapCase (str) { var theArray = str.split(""); theArray = theArray.map(function(ch) { var lower = ch.toLowerCase(); return ch === lower ? ch.toUpperCase() : lower; }); return theArray.join(""); } var str = "UPlowUPlow"; snippet.log("Before: " + str); str = swapCase(str); snippet.log("After: " + str); 
 <!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 

Here's the concise version:

 function swapCase (str) { return str.split("").map(function(ch) { var lower = ch.toLowerCase(); return ch === lower ? ch.toUpperCase() : lower; }).join(""); } var str = "UPlowUPlow"; snippet.log("Before: " + str); str = swapCase(str); snippet.log("After: " + str); 
 <!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 

sen.charAt(i).toUpperCase() does not change the character at the position. You need to manually update it.

function swapCase (str) {
  var sen = str;
  var updatedStr = "";
  for ( var i = 0; i < sen.length; i++) {
    if (sen.charAt(i) === sen.charAt(i).toLowerCase()) {
      updatedStr  += sen.charAt(i).toUpperCase();
    } else if (sen.charAt(i) === sen.charAt(i).toUpperCase()) {
      updatedStr  += sen.charAt(i).toLowerCase();
    }
  } 
  return updatedStr;
}

As a side note, remember that strings are immutable, quoting :

In JavaScript, strings are immutable objects, which means that the characters within them may not be changed and that any operations on strings actually create new strings. Strings are assigned by reference, not by value. In general, when an object is assigned by reference, a change made to the object through one reference will be visible through all other references to the object. Because strings cannot be changed, however, you can have multiple references to a string object and not worry that the string value will change without your knowing it

Thanks for the great answers, people!

Thanks to you guys, i "solved" this in seconds while i was working on it for a good amount of time.

I'd love to upvote your answers instead of writing a thank you note, but i'm not yet allowed to do that. So, i'll do it this way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM