How to find the largest and the smallest word in a string with TypeScript

1 Answer

0 votes
const s : string = "typescript php python c++ c c# java"
  
let max : string = "";
let min : string = s;
   
const words : string[] = s.split(" "); 
   
for (var i = 0; i < words.length; i++) {
    if (words[i].length > max.length)
        max = words[i];    
    if (words[i].length < min.length)
        min = words[i];
}
  
console.log("Largest word: " + max);
console.log("Smallest word: " + min);
     
     
          
  
/*
  
run:
  
"Largest word: typescript" 
"Smallest word: c" 
  
*/

 



answered Nov 12, 2022 by avibootz

Related questions

...