Reputation: 87
function alpha()
{
var a=prompt("Enter String: ");
for(var b=[i=0];c=a.charCodeAt(i);)
{
b[i++]=String.fromCharCode(c|(c>96 && c<123));
}
alert(b.join(""))
}
console.log(alpha());
So far some letters in the code change when I run this but for some reason it is not changing the string characters to all uppercase. (Example of output; I type "hi" into the prompt and the alert outputs "i" so the h disappears for some reason.
Upvotes: 2
Views: 4814
Reputation: 1
// code for change the string to lower case to upper case
const upperCase = ()=>{
let welcome = prompt("Enter your string");
let upper = '';
for(let i=0; i<welcome.length;i++){
code = welcome.charCodeAt(i);
if(code >= 97 && code <= 122){
let co = code-32;
console.log(co);
upper += String.fromCharCode(co);
}
else
{
upper +=String.fromCharCode(code) ;
}
console.log(upper);
}
}
Upvotes: 0
Reputation: 150078
Here's a way to implement it without calling any functions at all:
function myToUpper(string) {
var map = { a: 'A', b: 'B', c: 'C', d: 'D', e: 'E', f: 'F', g: 'G', h: 'H', i: 'I', j: 'J', k: 'K', l: 'L', m: 'M', n: 'N', o: 'O', p: 'P', q: 'Q', r: 'R', s: 'S', t: 'T', u: 'U', v: 'V', w: 'W', x: 'X', y: 'Y', z: 'Z' }
var output = ''
for (var i = 0; i < string.length; i++)
output += map[string[i]] || string[i]
return output
}
console.log( myToUpper('ABCdefGHIjkl123.') ) // 'ABCDEFGHIJKL123.'
Upvotes: 0
Reputation: 150078
Just for interest's sake, here's a way to do it in one statement without arrays or a loop (at least, not an explicit loop):
var input = 'ABCdefGHIjkl123.' // or you could prompt("Enter String: ")
var output = input.replace(/[a-z]/g, function(m) {
return String.fromCharCode(m.charCodeAt(0) - 32)
})
console.log(output)
Upvotes: 2
Reputation: 1419
Here's what I would do.
function alpha() {
var a = prompt("Enter string: ");
var upper = a.split('').map((e) => e.charCodeAt(0) > 96 && e.charCodeAt(0) < 123 ? String.fromCharCode(e.charCodeAt(0) - 32) : e);
return upper.join('');
}
console.log(alpha());
Upvotes: 2
Reputation: 986
Since this is a learning objective, I will only described a means to do this and let you try to figure it out using this answer as a guideline.
first you want to loop through the characters in string. for each character you would want some code to turn it into a capital letter, assuming it is a letter. finally you want to hold the capitalized letters in a variable.
This link may help for UTF-8 charcodes. Even if you aren't using utf-8, it may give you some ideas. http://www.utf8-chartable.de/
Upvotes: 1
Reputation: 16777
As Barmar mentioned in the comments above:
Subtract 32 from the character code if the character is between 97 and 123.
You can make your looping logic a little clearer with a split
-map
-join
combination.
Since you can also compare single-character strings in JavaScript, like 'a' < 'b'
, this can further reduce the amount of code needed to handle the conversion of character codes.
function alpha (s) {
return s.split('').map(function (c) {
return 'a' <= c && c <= 'z' ? String.fromCharCode(c.charCodeAt(0) - 32) : c
}).join('')
}
console.log(
alpha(prompt("Enter String: "))
)
Upvotes: 2
Reputation: 266
function alpha()
{
var a=prompt("Enter String: ");
var output="";
for(var x=0;x<a.length;x++)
output+=String.fromCharCode(a.charCodeAt(x)>96 && a.charCodeAt(x)<123 ? a.charCodeAt(x)-32: a.charCodeAt(x))
return output;
}
console.log("--" + alpha());
Just get the ascii code for each char, and if it ranges between 97 to 122 ( a to z ) deduct 32 from val to get the corresponding upper case char. Hope this helps.
Upvotes: 3