Programming Questions

  • Newest
  • Popular Tags
  • Ask 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 LongestWord(readline());
    Bjalla posted this question on 10/28/16 | javascript, longest, word
    Answers
  • +
  • 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; for(i=0;...)
  • +
  • 1
  • -
  • Try this: var LongestWord = (sen) => (sen.replace(/[^a-zA-Z0-9s]+/g,'').split(' ').reduce((a,v) => a.length > v.length ? a : v));
  • +
  • 1
  • -
  • 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.
  • +
  • 0
  • -
  • const LongestWord = (sen) => (sen.replace(/[^a-zA-Z0-9s]+/g,'').split(' ').reduce((a,v) => a.length > v.length ? a : v)); LongestWord(readline());
  • +
  • 0
  • -
  • instead of i<=arr.length use i<arr.length. 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 LongestWord(readline());
  • +
  • 0
  • -
  • Forget my older post, this is what I meant to post: function LongestWord(sen) { var replace_this = /[[email protected]#$%^&~*/(),.":{}|<>]/g; // replace every special character with an empty space var rep = sen.replace(replace_this, ""); // turn string into an array of words var arr = rep.split(" "); var greatest_length = 0; var longest_word = ''; for (var i = 0; i < arr.length; i++) { if (arr[i].length > greatest_length) { greatest_length = arr[i].length; longest_word = arr[i]; } else if (arr[i].length == greatest_length) { continue; } } return longest_word; } // keep this function call here LongestWord(readline());
  • +
  • -1
  • -
  • Here is what I did: function LongestWord(sen) { var replace_this = /[[email protected]#$%^&*?:(),.{}<>]/g; // replace every special character with an empty space var rep = sen.replace(replace_this, ""); // turn string into an array of words var arr = rep.split(" "); var greatest_length = 0; var longest_word = ''; for (var i = 0; i < arr.length; i++) { if (arr[i].length > greatest_length) { greatest_length = arr[i].length; longest_word = arr[i]; } else if (arr[i].length == greatest_length) { continue; } } return longest_word; } LongestWord(readline());
    Log in to write an answer.