Alternative to toUppercase():
To determine if a character is uppercase or lowercase, you can use the following code snippet:
str[i].charCodeAt(0) > 96
For more information on the charCodeAt function, check out this link.
If you are interested in fromCharCode function details, visit this page.
Using while loop efficiently:
var i = 0;
while (i < str.length) ...
Avoiding push function for arrays:
UPPERCASE[UPPERCASE.length] = uppercase(names[i]);
I trust that this function will be beneficial for your needs.
function uppercase(str) {
var i = 0;
var output = "";
while (i < str.length) {
var x =
(str[i].charCodeAt(0) > 96)
? String.fromCharCode(str[i].charCodeAt(0) - 32)
: String.fromCharCode(str[i].charCodeAt(0));
output += x;
i++;
}
return output;
}
Example:
function uppercase(str) {
var i = 0;
var output = "";
while (i < str.length) {
var x =
(str[i].charCodeAt(0) > 96) ?
String.fromCharCode(str[i].charCodeAt(0) - 32) :
String.fromCharCode(str[i].charCodeAt(0));
output += x;
i++;
}
return output;
}
var UPPERCASE = [];
var names = ["rOb", "dwayne", "james", "larry", "steve"];
var i = 0;
while (i < names.length) {
UPPERCASE[UPPERCASE.length] = uppercase(names[i]);
i++;
}
console.log(UPPERCASE);
Updated Version:
Solution without using a function and implementing linear condition technique.
var UPPERCASE = [];
var names = ["rOb", "dwayne", "james", "larry", "steve"];
var i = 0;
while (i < names.length) {
var j = 0,
output = "",
str = names[i];
while (j < str.length) {
var x;
if (str[j].charCodeAt(0) > 96) {
x = String.fromCharCode(str[j].charCodeAt(0) - 32);
} else {
x = String.fromCharCode(str[j].charCodeAt(0));
}
output += x;
j++;
}
UPPERCASE[UPPERCASE.length] = output;
i++;
}
console.log(UPPERCASE);