agareis / coding-journey

Simple Demo ShowCases on One Pager
0 stars 0 forks source link

Add error handling #3

Closed idleherb closed 7 years ago

idleherb commented 7 years ago

Error Description

  1. Load page, open console
  2. Enter 1 character in textarea -> error appears in console

No matter what input the textbox has, no errors should appear in the console -> add error handling for such cases.

Bonus: when user focuses textarea and starts to enter characters in the textbox, "Infinity" appears -> maybe a maximum value or a text explaining what is happening would be better

idleherb commented 7 years ago

try ... catch ... finally:

agareis commented 7 years ago

Thank you for the hint. You nailed it!!! I should read more carefully ;)!

idleherb commented 7 years ago

@agareis just as a comment:

Current code:

try {
  return str.match(/[\w0-9]{7,}/gi).length;
}
catch (error){
  console.log("Something went wrong ", error);
  return 0;

Possible alternative, without using try... catch:

var match = str.match(/[\w0-9]{7,}/gi);
if (match === null) {  // No long words were found
    return 0;
}
return match.length;
idleherb commented 7 years ago

@agareis In general, "Something went wrong" is also not a good error message. It is a sign that you don't know what went wrong ;) What went wrong in the above case is that the regular expression did not return any matches because there were no long words in the text.

agareis commented 7 years ago

You have a strong point here! And again I fell into your trap ;)! Lesson learned: If I plagiarise I should be aware of the consequences hearing and feeling from my dear mentor :D!!!