Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Validation of duplicates and empty strings - My solution

Here's my snapshot of the validation part of the extra challenge. I didn't see a complete solution so I played around with a with a few things I'd seen.

https://w.trhou.se/aac59a0rro

Anyone have a suggestion to refactor or improve in some way?

Thanks

2 Answers

Steven Parker
Steven Parker
243,266 Points

Looks pretty good! :+1:

But the test for an empty string should be done outside the loop, otherwise it will not be done at all for the first item, and will be done redundantly multiple times when there are several items.

Also, in the case of duplicate, you might still want to clear the input box before you return.

Thanks Steven,

I hadn't tested an empty string the first input. Am I right in thinking that it wouldn't run because there were no lis to loop through therefore it wouldn't run until an li was created? This obv wouldn't be an issue with the duplicate test.

Also does it matter which order the duplicate or empty string test is placed? I'm guessing it doesn't but I'd like some clarification.

Here's an updated snapshot.

https://w.trhou.se/vu5836dg02

Thanks for the input!

Steven Parker
Steven Parker
243,266 Points

You are quite right, when there were no entries, none of the loop code ran including the empty test.

And it doesn't matter for functionality which order the tests are done, but for efficiency it might be good to do the empty test first, since it is only done once.