How to find the longest string passed as an argument to function in JavaScript

1 Answer

0 votes
function longestString() {
    let longeststr = "";
    
    for (let i = 0; i < arguments.length; i++) {
        if (arguments[i].length > longeststr.length) {
            longeststr = arguments[i];
        }
    }
  
    return longeststr;
}

console.log(longestString("aa", "cccc", "d", "fff"));



      
/*
run:
      
cccc

*/

 



answered May 3, 2024 by avibootz
...