Programming Questions

  • Newest
  • Popular Tags
  • Ask A Question
  • JavaScript Longest Word Error
    Hi everyone! I have been trying to find my own solution for the Longest Word challenge, but I always get a syntax error saying: "missing ')' after argument list". I really can't figure out what the problem with my code is, so maybe you can help me?
    function LongestWord(sen) { 
        var arr = sen.match(/[a-z0-9]+/gi);
        var a = "";
        for (i = 0; i <= arr.length; i++) {
            if (arr[i].length > a.length) {
                a = arr[i];
        return a;
    // keep this function call here 
    Bjalla asked this question on 10/28/16 | javascript, longest, word
  • +
  • 2
  • -
  • Your function does not run in a generic browser console. However the output does not indicate "missing ')' after argument list" for some guidance in finding your code problem, try this at the console: > var arr = ['a', 'b', 'c'] > arr.length > arr[0] > arr[1] > arr[2] > arr[arr.length] See what the console returns for the input above, then look again at your code. Good Luck!
  • +
  • 2
  • -
  • On a side note you should really declare your variables, even those in for loops. Although JS doesn't throw an error for your i variable in your loop, the variable i becomes a global variable. Therefore if you use i somewhere else in your code it could behave in a way you didn't expect. You should try and keep global space as unpolluted as possible, otherwise finding bugs will be a nightmare. So if you wrote
    for(var i=0;...)
    the i variable will only have scope within the function it resides in. You can also declare i before you use it in the loop
    function example(arg){
    var i;
  • +
  • 0
  • -
  • I just had this problem as well. My issue was that when I changed the string input in the parameter testing box I forgot to put quotes around the input.
    Log in to write an answer.