Swap Case on javascript
Like Ian said, you need to build a new string.
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"
Swap Cases- switch from lowercase to uppercase and viceversa
There are multiple issues with this code,
The array should not run to length, so it should be
<
instead of<=
There is no need for 3 loops
result.join should join with an empty string instead of blank space
Intendations are all over the place.
see corrected code below:
function SwapCase(str) {
let cap=str.toUpperCase()
let low=str.toLowerCase()
let arr=str.split("")
let result=[]
for(var i=0; i<arr.length;i++){
if(arr[i]===cap[i]){
result.push(arr[i].toLowerCase())
}
if(arr[i]===low[i]){
result.push(arr[i].toUpperCase())
}
}
return result.join("")
}
console.log(SwapCase("heLLo"))
Swapcase Javascript
Your code has a few errors:
- JavaScript is case-sensitive: is the function called
swapcase
orswapCase
? string
isn't a great variable name, especially because you refer to it by a different name (letters
) at the end of the function.- Your comparison operators are inverted. Try
>= 'a'
,<= 'z'
, etc. - JavaScript is case-sensitive: you should be calling
toUpperCase
andtoLowerCase
. - You're never calling
swapCase
aside from inside the function itself.
The fixed version:
var swapCase = function(str) {
var letters = str.split("");
for (var i = 0; i < letters.length; i++) {
if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {
letters[i] = str.charAt(i).toUpperCase();
} else if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {
letters[i] = str.charAt(i).toLowerCase();
}
}
str = letters.join("");
console.log(str);
return str;
};
var text = "Life is 10% what happens to you, and 90% of how you REACT to it";
swapCase(text);
An easier way to test the case of a character would be:
if (str.charAt(i).toLowerCase() === str.charAt(i)) {
// Character is lower case or not a letter
} else {
// Character is upper case
}
More efficient JS case swap
It sounds like: for every character, I want to make it Upper if it's Lower, and Lower if it's Upper.
That's very easy:
function swapCase(str) {
var out = '';
for (var i=0; i<str.length; i++) {
// It's lower, or non-A-Z
if (str[i].toLowerCase() == str[i]) {
out += str[i].toUpperCase();
}
else {
out += str[i].toLowerCase();
}
}
return out;
}
alert(swapCase('Hello World Sup Bro My Name iS Richard!'));
(And the Fiddle to prove it.](http://jsfiddle.net/rudiedirkx/m4mhsyuz/)
How to change every character to opposite case in Javascript?
Strings are immutable, they don't change when using methods like toLowerCase()
, they return a new string that is changed, and you have to assign that new string to something :
function changeCase() {
var str = document.getElementById("changeCase").innerHTML,
str2 = '';
for (var i = 0; i < str.length; i++) {
if (str.charAt(i) === str.charAt(i).toLowerCase()) {
str2 += str.charAt(i).toUpperCase();
} else if (str.charAt(i) === str.charAt(i).toUpperCase()) {
str2 += str.charAt(i).toLowerCase()
} else {
str2 += str.charAt(i);
}
}
console.log(str2, "after");
}
FIDDLE
Related Topics
Batchsize Field Name Ignored in Field Projection
Switch Case Not Showing Correct Results
How to Create a Simple Http Proxy in Node.Js
Remembering State Before App Goes in Foreground
Auto-Refreshing Div with Jquery - Settimeout or Another Method
Javascript/Jquery Check Broken Links
JavaScript Fuzzy Search That Makes Sense
Nested Element (Web Component) Can't Get Its Template
Add Cors Header to an Http Request Using Ajax
How to Detect with JavaScript/Jquery If the User Is Currently Active on the Page
Regex for Number with Decimals and Thousand Separator
Accessing the Content of Other Tabs in a Browser
Is It a Good Idea to Learn JavaScript Before Learning Jquery
What Are Alternatives to Extjs
Injecting Js Functions into the Page from a Greasemonkey Script on Chrome