How to check if two strings have the same number of words in TypeScript

1 Answer

0 votes
function two_strings_have_same_number_of_words(str1: string, str2: string) : boolean {
    const wordsOfStr1: string[] = str1.split(" ");
    const wordsOfStr2: string[] = str2.split(" ");
   
    return wordsOfStr1.length === wordsOfStr2.length;
}
 
const str1: string = "typescript java c++ go";
const str2: string = "c python c# assembler";
 
if (two_strings_have_same_number_of_words(str1, str2)) {
    console.log("yes");
} else {
    console.log("no");
}
 
  
   
/*
run:
      
"yes" 
      
*/

 



answered May 9, 2024 by avibootz
...