5. Strings

Que.1 Count Characters

            
  const countCharacters = (S) => 
  {
    let aCount = 0;
    let dCount = 0;
  
    for (let i = 0; i < S.length; i++) {
      if (S[i] === "A") {
        aCount++;
      } else if (S[i] === "D") {
        dCount++;
      }
    }
  
    return [aCount, dCount];
  }; 
        

Que.2 Count the Heads

            
  var Count_Occ = (s) => 
  {
    let count = {};
      for (let i = 0; i < s.length; i++) {
          let char = s.charAt(i);
          if (count[char]) {
              count[char]++;
          } else {
              count[char] = 1;
          }
      }
      let result = "";
      let keys = Object.keys(count).sort();
      for (let i = 0; i < keys.length; i++) {
          if (count[keys[i]] > 1) {
              result += keys[i] + count[keys[i]];
          }
      }
  
      return result;
  }; 

        

Que.3 Count the Vowels

            
  var Count_Vowels= (S) => 
  {
      let count = 0;
      for(let i=0; i < S.length; i++){
        if(S[i]==='a' || S[i]==='e' || S[i]==='i' || S[i]==='o' || S[i]==='u'){
          count++;
        }
      }
      return count;
  };
        

Que.4 Concatenate the Strings

            
  var Concatenate_Strings = (S1, S2) => 
  {
      return S1.concat('', S2);
  };
        

Que.5 Find Length

            
  const findLength = (S) => 
  {
      return S.length;
  };
        

Que.6 Find the Winner

             
  var Game_Winner = (S) => 
  {
    let adityaWins = 0;
      let danishWins = 0;
      
      for(let i = 0; i < S.length; i++) {
          if(S[i] === 'A') {
              adityaWins++;
          } else {
              danishWins++;
          }
      }
      
      if(adityaWins > danishWins) {
          return 'Aditya';
      } else if(danishWins > adityaWins) {
          return 'Danish';
      } else {
          return 'Draw';
      }
  } 
        

Que.7 Join Strings

            
  const joinStrings = (S, P) => 
  {
      return S+P;
  };
        

Que.8 Plaindrome Check

  var Palin_Check = (str) =>
  {
  	// Remove all non-alphanumeric characters from the string
    str = str.replace(/[^0-9a-zA-Z]/g, '');
  
    // Check if the string is equal to its reverse
    if (str === str.split('').reverse().join('')) {
      return "True";
    } else {
      return "False";
    }
  }
        

Que.9 Reverse the String

  var Reverse_String = (str) => 
  {
      let str2 = "";
      for(let i = str.length-1; i>=0; i--){
        str2 +=str[i];
      }
      return str2;
  }; 
        

Que.10 Match the Strings

  var String_Match = (S1,S2) => 
  {
      if(S1===S2){
        return "YES";
      }
      else{
        return "NO";
      }
  };
        

Que.11 String Replace

 
  var Replace = (S, pattern , text) => 
  {
      return S.replace(pattern, text);
  };
        

Que.12 Split the String

            
  var Split_the_string = (S) => 
  {
    // Split the string by spaces using the split() method
    // Return the array of strings
    return S.split(" ");     
  };
 
        

Que.13 Count the Vowels and Consonants

  
  var Count_Vowels= (S) => 
  {
      Count_V=0;
      for(let i=0; i < S.length; i++){
        if(S[i]==='a'||S[i]==='e'||S[i]==='i'||S[i]==='o'|| S[i]==='u'){
         Count_V++;
        }
      }
      return  Count_V;
  };
  var Count_Consonants= (S) => 
  {
      Count_C=0;
      for(let i=0; i < S.length; i++){
        if(S[i]!=='a' && S[i]!=='e' && S[i]!=='i' && S[i]!=='o' && S[i]!=='u'){
          Count_C++;
        }
      }
      
      return  Count_C;
  };
  
        
Back
Next