4

I am a beginner in Javascript.
I tried to make a function about covert string into uppercase or lowercase. But I am confused why it can get expected output. Following is my function purpose and codes.Thank you!

  1. Function purpose :

When letter in string is uppercase, it will change into lowercase. When letter in string is lowercase, it will change into uppercase. For example: "Peter" will transfer into "pETER"

  1. Question:

I can't understand why my code ends up with "Peter" rather then "pETER"

function swap(str) {
 var name = ''
 for (i = 0; i <= str.length - 1; i++) {
 if (str[i] >= 'a' && str[i] <= 'z') {
 str[i].toUpperCase()
 } else {
 str[i].toLowerCase()
 }
 name += str[i]
 }
 return name
}
console.log(swap('Peter'))

I am not sure whether the problem is in this line.

if(str[i] >= 'a' && str[i] <= 'z'){
 str[i].toUpperCase()
}

Can anyone help me , thanks!!

asked Aug 15, 2020 at 8:44
4
  • 2
    Strings are immutable in JS, so doing str[i].toUpperCase() won't make the i-th letter in your string uppercase, instead, it will return the uppercase version, which you can use to build a new string. Commented Aug 15, 2020 at 8:48
  • Does this answer your question? convert uppercase and lowercase in javascript Commented Aug 15, 2020 at 8:48
  • 1
    @NickParsons Thank you so much ! I know what my problem is. Commented Aug 15, 2020 at 12:05
  • 1
    @HarmandeepSinghKalsi Yes!! it helps me, sorry I haven't found this useful answer before. Commented Aug 15, 2020 at 12:16

5 Answers 5

2

I think your problem is to think that str[i].toUpperCase() or str[i].toLowerCase() will change the value of the str[i], but it doesn't. These functions will change the char value to uppercase or lowercase and they'll return the result of the function call, but the original variable (str[i]) will remain its value.

Try with this version:

function swap(str) {
 var name = ''
 var string;
 for (i = 0; i <= str.length - 1; i++) {
 string = str[i]; 
 if (str[i] == string.toUpperCase()) {
 name += string.toLowerCase();
 } else {
 name += string.toUpperCase();
 }
 }
 return name;
}
console.log(swap('PeTeR'));

answered Aug 15, 2020 at 9:19
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! You are right ! I think it will change the original str[i] value, but i ignore that strings are immutable in JS.
2

Use reduce to accumulate your new string and lowercase/upercase JS functions to check your letters hence do the conversion.

const swap = (str) => str.split('').reduce((acc, char) =>
 acc += char === char.toLowerCase() ? char.toUpperCase() :
 char.toLowerCase(), '')
console.log(swap('Peter'))

answered Aug 15, 2020 at 9:01

Comments

0

This works:

function swap(str) {
 var name = ''
 for (i = 0; i <= str.length - 1; i++) {
 if (str[i] >= 'a' && str[i] <= 'z') {
 name += str[i].toUpperCase()
 } else {
 name += str[i].toLowerCase()
 }
 }
 return name
}
console.log(swap('Peter'))

Harmandeep Singh Kalsi
3,3652 gold badges16 silver badges31 bronze badges
answered Aug 15, 2020 at 8:55

Comments

0

I have changed your code but it should work now

function swap(str) {
 var name = ''
 var lowers = "abcdefghijklmnopqrstuvwxyz";
 for (i = 0; i <= str.length - 1; i++) {
 if (lowers.includes(str[i])) {
 name +=str[i].toUpperCase()
 } else {
 name +=str[i].toLowerCase()
 }
 }
 return console.log(name);
 }
swap("Peter");

Harmandeep Singh Kalsi
3,3652 gold badges16 silver badges31 bronze badges
answered Aug 15, 2020 at 8:57

1 Comment

Thank you ! I know where the problem is.
0
function name(str){
 let newArr = str.split('')
 let output = [];
 for(let x of newArr){
 output.push(x.toUpperCase());
 }
 return output.join('');
}
name('hello')
answered Feb 27, 2021 at 8:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.