-1

function findUpper(text) {
    let arr = [];

    if (text.length === 0) {
        return arr;
    }

    if (text.charAt(0) === text[0].toUpperCase()) {
        arr.push(text[0]);
    }

    arr = arr.concat(findUpper(text.slice(1)));
    console.log(arr);
    return arr;
}

findUpper("i am a Web developer Student");

The desired output is "W", since it is the first upper case letter, But I cannot figure out how to print out that result.

MORÈ
  • 2,480
  • 3
  • 16
  • 23
IanA
  • 1
  • 1

2 Answers2

0

You need to avoid whitespace,since whitespace uppercase is equal to itself,also there is no need to use recursive function,just a iteration can do it

function findUpper(text){
    let arr = [];
    
    if(text.length === 0){
        return arr;
    }
    for(let i=0;i<text.length;i++){
       let txt = text.charAt(i)
       // using txt.trim().length > 0 to remove whitespace
       if(txt.trim().length > 0 && txt === text[i].toUpperCase()){
        arr.push(txt);
       }
    }
    return arr;
}
   



console.log(findUpper("i am a Web developer Student")[0]);

Update: another way is to using split() first and then using iteration to find it

function findUpper(text){
    let arr = [];
    
    if(text.length === 0){
        return arr;
    }
    text = text.split(" ").join("")
    for(let i=0;i<text.length;i++){
       let txt = text.charAt(i)
       if(txt === text[i].toUpperCase()){
        arr.push(txt);
       }
    }
    return arr;
}
   
console.log(findUpper("i am a Web developer Student")[0]);
flyingfox
  • 13,414
  • 3
  • 24
  • 39
  • Thank you so much! This is very helpful. I was trying to solve this for more than a week now. Again, Thank you for the help :) – IanA Nov 17 '22 at 06:13
0

From the sentence you need to take out the words first and then check the uppercase for the first letter of the word, which can be done as

function findUpper(text) {
let arr = text.split(" ")

var upperCaseWord = ""
if (arr.length == 0) {
return upperCaseWord;
}   
for(let i = 0; i < arr.length; i++) {
let word = arr[i];
let firstLetter = word.charAt(0)
if(firstLetter == firstLetter.toUpperCase()) {
upperCaseWord = word;
break;
}
}
return upperCaseWord;
}
console.log(findUpper("i am a Web developer Student"));
vignesh
  • 994
  • 10
  • 12