1

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,155
  • 14
  • 29
  • 44
Fahad Mir
  • 53
  • 6

4 Answers4

4

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
  • 4,800
  • 3
  • 26
  • 31
Shubh
  • 8,368
  • 4
  • 18
  • 39
  • 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 – Shubh 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 – Shubh 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,155
  • 14
  • 29
  • 44
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
  • 263,463
  • 30
  • 215
  • 251
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
  • 30,747
  • 12
  • 76
  • 133