2

I am try to pick capital characters from the string with the help of a function and for loop but i can't figure out how i can do it i try using toUpperCase as you see it in the code but it is not work any idea how i can do it ?

function onlyCapitalLetters(cap){
    var string = "";
    for(var i = 0; i < cap.length; i++){
        if(cap[i] === cap.toUpperCase()){
            string += cap[i];
        }
    }
    return string;
}

onlyCapitalLetters("Apple");
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Fahad Mir
  • 63
  • 6

5 Answers5

5

You can try the regex, with String.prototype.match to return capital letters only:

function onlyCapitalLetters(cap){
    return  cap.match(/[A-Z]/g, "").join(''); // join the array to return a string
}

console.log(onlyCapitalLetters("Apple"));
console.log(onlyCapitalLetters("BUTTerfly"));
console.log(onlyCapitalLetters("LION"));
Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
Shubham Dixit
  • 9,242
  • 4
  • 27
  • 46
  • 4
    But its the solution right?? ,one of the possible solution may be easy .It doesn't deserves downvote even if it ddeosn't deserves upvote @axiac – Shubham Dixit Jul 22 '19 at 12:39
  • Kindly comment after down vote a answer. it helpful for the user. – Velusamy Venkatraman Jul 22 '19 at 12:39
  • I didn't vote this answer (neither up or down). But it qualifies for a down vote. It is not useful because it does not provide a solution for the question. The function in the question returns a string that contains only uppercase letters. The function suggested by this answer returns a boolean. – axiac Jul 22 '19 at 12:45
  • 1
    @axiac previously it was returning array ,now with the edit suggested by Dadboz it returns string now – Shubham Dixit Jul 22 '19 at 12:47
  • @Shubh i also noticed that. as per this @axiac you did not run this code ever. but deside its unqualified. his old core is `console.log(onlyCapitalLetters("LION"));` – Velusamy Venkatraman Jul 22 '19 at 12:50
1

Can you try like this

function findUpcase(value){
    input = value
    data = ""
    input.split("").map(res => {
        if(res == res.toUpperCase()){
            data = data+ res
        }
    })
    return data
}

console.log( findUpcase("MyNameIsVelu") );
//'MNIV'
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
0

As noted in comments you need to change cap.toUpperCase() to cap[i].toUpperCase().

But you can do it with just one replace:

console.log('Apple Orange'.replace(/[^A-Z]/g, ""));
trincot
  • 317,000
  • 35
  • 244
  • 286
0

It is possible to use replace method with Regex to eliminate numbers and letters written in lowercase:

let str = 'T1eeeEeeeSssssssTttttt';
let upperCase = str.replace(/[a-z0-1]/g, '')
console.log(upperCase);
StepUp
  • 36,391
  • 15
  • 88
  • 148
0

Please Use Below code to get Capital letter of the sentence :

Demo Code

var str = 'i am a Web developer Student';
var sL = str.length;
var i = 0;
for (; i < sL; i++) {
    if (str.charAt(i) != " ") {
      if (str.charAt(i) === str.charAt(i).toUpperCase()){
        console.log(str.charAt(i));
        break; 
      }
    }
}
Swadesh Ranjan Dash
  • 544
  • 1
  • 4
  • 17