Last active
March 13, 2020 03:26
-
-
Save digitaldrummerj/727c1543ec93b0ea9bcab4c9f187d85e to your computer and use it in GitHub Desktop.
JS: Convert String to Upper or Lower Based on Even or Odd without using ToUpperCase/ToLowerCase
This file contains hidden or 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 output(str, pos) { | |
var result = '' | |
for (var i = 0; i <= str.length; i++) { | |
var mod = i % 2; | |
var code = str.charCodeAt(i); | |
if (pos === 'even' && mod === 0) { | |
result += changeChar(code); | |
} else if (pos === 'odd' && mod !== 0) { | |
result += changeChar(code); | |
} else { | |
result += String.fromCharCode(code); | |
} | |
} | |
return result | |
} | |
function changeChar(code) { | |
if (code > 64 && code < 91) { | |
return String.fromCharCode(code + 32) | |
} else { | |
return String.fromCharCode(code - 32) | |
} | |
} | |
document.write('odd (aZbe to azbE): ' + output('aZbe', 'odd')); | |
document.write('<br />even (aZbe to AZBe): ' + output('aZbe', 'even')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment