Skip to content

Instantly share code, notes, and snippets.

@digitaldrummerj
Last active March 13, 2020 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digitaldrummerj/727c1543ec93b0ea9bcab4c9f187d85e to your computer and use it in GitHub Desktop.
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
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