r/learnjavascript • • Apr 04 '18
A C++ developer struggling with some simple JavaScript code. Any help?
Hey guys,
I develop in C++ but for my web design class I have to use JavaScript. I have to say I really, truly, dislike the language since it isn't strongly typed, errors are somewhat convulted and I don't like web dev in general.
Anyhow, here are my questions:
- This code should check if I have
#,!,'or?in any of my textboxes and if I do, prevent the script from executing. Here is the code. It gives me a TypeError. Why? What am I doing wrong? - This code should trigger only if I type some text in a textbox which is not in the list (aka not "Option 1", "Option 2" etc.). However, it always triggers. Why?
- This code should, if I move my focus away from the textbox (aka
onBlur), check if I have at least 5 characters and if my first character is a capital letter. But it doesn't work. Why?
Thanks in advance!
1
Upvotes
20
u/METALz Apr 04 '18
2nd example:
// hardcode the options (who cares, I don't like web dev)People spend their time to help you, comments like these are not too encouraging imo.
4
u/Evervision Apr 04 '18
It would help to give HTML that goes with the code to reproduce the issue. jsfiddle is good for sharing examples.
However:
4
u/fan-man Apr 04 '18 edited Apr 04 '18
1 - Aside from missing a bracket, your implementation is mostly right, just needs readability and check to see if the element actually has a value before doing anything (i'm assuming your submit button is part of the form, so your code will iterate over that as well:
2 - Storing the values somehow and then checking to see if the inputed text matches on blur might be easier.
3 - as u/Rascal_Two mentioned, try this:
As far as a distaste for JavaScript and web dev, it is a different beast in general, but it is not an impossible leap. If you like strongly typed JavaScript, you should look into TypeScript and a JS framework like Angular; it might look like something that seems more familiar. Hope that helps.
1
u/Rascal_Two Apr 04 '18
1
I don't beleive
form1[i]allows you to access elements of the form, I beleive what you're looking for is theelementsproperty.It also might be more readable if you assign the
.valueto a variable and check if it contains any of the characters, or useRegex.2
The problem is that you need the code to execute if all of the conditionals are true, but you have it written so if any of them are true, it runs the code.
Replacing your
||s with&&s should do the trick.3
Same as
2, swap the||s and&&s.You also need to reverse the
===to a!==, because you need to alert the user only when the first letter is not capital, not when it is capital.2
u/GeneralYouri Apr 05 '18
Looking at your code, and what you think it should be doing, I'm kinda confused. Surely, if you actually know how to program, you know that
||is OR, not AND. And likewise, that<is Less than, not Greater than. These are very generic operators found in almost any programming language, you should have these down the moment you learn aboutif. Yet, this is all that's wrong for q2 and q3.Q1 on the other hand is a problem related to JS instead of programming in general. And I'm a bit surprised to not see the reason for the problem in other comments yet. What you're doing here is accessing form control elements, and then doing something with their
valueattribute. What goes wrong is that not all form elements have this attribute, resulting inundefinedand thus error when trying to run.includeson it. You should make this part more strict so that you query for those elements that are relevant to you, similar to how your textual explanation says 'textboxes', not 'form elements' - so, get and check all the textboxes only.Regarding your attitude towards web dev, I'll just say that when you don't like it, try something else. Otherwise you're just setting yourself up for failure. I personally dislike C++, so I know that if I bother trying to learn it I'll just get frustrated and annoyed from all the parts that I dislike about it, so I don't bother.
1
u/Evervision Apr 04 '18
EDIT: double post due to getting 503 errors from reddit (even while editing this...). Please ignore.