Created
May 21, 2022 18:16
-
-
Save ZakriaJanjua/3d04286c4509ca95e7ceb65d0583deec to your computer and use it in GitHub Desktop.
Take a string and invert the case of each character. If the English characters in the string are wrapped between numbers then switch the numbers and if there is any other character then do not switch them.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function StringChallenge(str) { | |
// code goes here | |
let result = "" | |
str = reversing(str) | |
for (let i = 0; i < str.length; i++) { | |
let current = str[i].charCodeAt(0) | |
if (current >= 65 && current <= 90) { | |
result = result + str[i].toLowerCase() | |
} else if (current >= 97 && current <= 122) { | |
result = result + str[i].toUpperCase() | |
} else { | |
result = result + str[i] | |
} | |
} | |
return result; | |
} | |
function reversing(str) { | |
let result = "" | |
let checkArr = [] | |
for (let i = 0; i < str.length; i++) { | |
let current = str[i].charCodeAt(0) | |
if (current >= 48 && current <=57) { | |
checkArr.push(str[i]) | |
for (let j = i+1; j < str.length; j++) { | |
let currentJ = str[j].charCodeAt(0) | |
if ((currentJ >= 65 && currentJ <= 90) || (currentJ >= 97 && currentJ <= 122)) { | |
checkArr.push(str[j]) | |
} else if (currentJ >= 48 && currentJ <=57) { | |
checkArr.push(str[j]) | |
let temp = checkArr.pop() | |
checkArr.push(checkArr[0]) | |
checkArr[0] = temp | |
console.log(checkArr) | |
checkArr = checkArr.join('') | |
result = result + checkArr | |
i = j | |
checkArr = [] | |
} else { | |
checkArr.push(str[j]) | |
checkArr = checkArr.join('') | |
result = result + checkArr | |
i = j | |
checkArr=[] | |
} | |
} | |
} else { | |
result = result + str[i] | |
} | |
} | |
return result | |
} | |
console.log(StringChallenge("Hello 3d4jrm -5")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment