How to find the largest and the smallest word in a string with Node.js

1 Answer

0 votes
const s = "node.js php python c++ c c# java"
  
let max = "";
let min = s;
   
const words = 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: node.js
Smallest word: c
  
*/

 



answered Nov 12, 2022 by avibootz
...