so I've actually came into this quation in a javascript tutorial by rithm school(which is very nice by far): Write a function called onlyCapitalLetters which accepts a string and returns a new string with only the capital letters passed to the string. this is the requested output:
onlyCapitalLetters("Amazing") // "A"
onlyCapitalLetters("nothing") // ""
onlyCapitalLetters("EVERYTHING") // "EVERYTHING"
thye've got this solution, but it's using this .charCodeAt() method which they haven't talked about before, and also seems pretty sophisticated. right now I am reading a lot of documentation related to it but still can't dig it.
before I've gine into their solution, Iv'e tried something else by myself, which was based about the things that were already introduce in this tutorial.
rithm school's solution:
function onlyCapitalLetters(str){
var newStr = '';
for(var i = 0; i < str.length; i++){
if(str[i].charCodeAt(0) < 91 && str[i].charCodeAt(0) > 64 ){
newStr += str[i];
}
}
return newStr;
}
my attempt:
function onlyCapitalLetters(string) {
var newString = '';
for(i=0;i<string.length;i++) {
if(string[i] === string[i].toUpperCase) {
} newString += string[i];
}
return newString;
}
although, when I've thrown an string as an argument, such as
onlyCapitalLetters('TbRnY OnUmT');
it gave me back the string as is, insted of a new, only uppercase string.
"TbRnY OnUmT"
is there a way, more in this simplified direction I've tried, to go for making this function happen? in addition, does anyone has some insights about this charCodeAt and their whole solution in general? This is my first post, so thank you for your help anyway!