You will be given a string with mixed upper and lower case letters. Write a program to switch uppercased letters to lowercased letters, and lowercased letters to uppercased letters.
Result Example
Solution
- Declare a variable named 'answer' and store an empty string.
- Use for...of loop to iterate through the string and compare all of the letters with the uppercased version of itself. Use the strict equality operator(===) here. If they match, it means the letter is uppercased. Change it to the lowercased letter with toLowerCase() method and add it to the 'answer' string. If they don't, it means the letter is lowercased so switch it to the uppercased letter using toUpperCase() method and add it to the 'answer' string.
- Return 'answer'.
function solution(str){
let answer = '';
for (let x of str){
if (x === x.toUpperCase()) {
answer += x.toLowerCase();
} else {
answer += x.toUpperCase();
}
}
return answer;
}