Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- The distance between upper case characters and lower case characters in standard ASCII is 32.
- A : 65 a : (65 + 32) = 97
- B : 66 b : (66 + 32) = 98
- C : 67 c : (67 + 32) = 99
- ...
- */
- function convertToUpperCase(str) {
- var result = ''
- for (var i = 0; i < str.length; i++) {
- var code = str.charCodeAt(i)
- if (code > 97) {
- result += String.fromCharCode(code - 32)
- } else {
- result += str.charAt(i)
- }
- }
- return result
- }
- function convertToLowerCase(str) {
- var result = ''
- for (var i = 0; i < str.length; i++) {
- var code = str.charCodeAt(i)
- if (code > 64 && code < 91) {
- result += String.fromCharCode(code + 32)
- } else {
- result += str.charAt(i)
- }
- }
- return result
- }
- function converter(str, whichCase, invert) {
- if (whichCase === "low") {
- str = convertToLowerCase(str)
- } else {
- str = convertToUpperCase(str)
- }
- //String without first symbol
- var strNoFrst = str.substring(1, str.length - 1)
- if (invert === true) {
- code = str.charCodeAt(0)
- if (code > 64 && code < 91) {
- result = String.fromCharCode(code + 32)
- } else {
- result = String.fromCharCode(code - 32)
- }
- str = result + strNoFrst
- }
- return str
- }
- console.log (converter('ABdddDDsdfdfFDsdfDdC', 'up', false))
- console.log (converter('ABdddDDsdfdfFDsdfDdC', 'low', false))
- console.log (converter('ABdddDDsdfdfFDsdfDdC', 'up', true))
- console.log (converter('ABdddDDsdfdfFDsdfDdC', 'low', true))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement