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!
- 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"
- 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!!