NewStats: 3,264,677 , 8,184,395 topics. Date: Thursday, 12 June 2025 at 12:10 AM 5x63186z3e3g |
HELP Javascript Devs! How Do I Re-write This If---else--if Statements To Run? (1303 Views)
(1) (Go Down)
MidasKinki: 1:53am On Aug 06, 2022 |
The two Else-ifs aren't working together. Each of them works if remove the other one. Only the last part works if I include both of them. Is there anything wrong with the syntax? Do you have a better way I can achieve the same result? Please help.
|
thug: 4:44am On Aug 06, 2022 |
Numbers and 11 cannot be strings. Remove the quotations marks.....
3 Likes |
etoluw: 7:13am On Aug 06, 2022 |
MidasKinki: typeof inputPhone.value will always be a string whether you put number or string so typeof inputPhone.value != number will always be true if you need further explanations you can send whatsapp message |
emmancipated(m): 7:32am On Aug 06, 2022 |
typeof inputPhone.value != Number inputPhone.value.length < 11 You can also just use <input type="number"> as the tag in your html if you expect just phone numbers and save your self some stress. 2 Likes |
Anonime1105(m): 8:42am On Aug 06, 2022 |
MidasKinki: 1. "numbers" should be Number without the quote with a capital N 2. Remove the quote from "11", putting it in between quotes makes it a string instead of a number that it's supposed to be. |
etoluw: 8:59am On Aug 06, 2022 |
Anonime1105: Sir, the first paragraph is wrong typeof operator will return "number" when it is a number(primitive) 1 Like |
jikins(m): 9:32am On Aug 06, 2022 |
emmancipated: Input type of number doesn't work as expected on Firefox from my experience and input generally returns a string no matter what you to it. Checking if the value is a number will always return false. op do if (isNaN(Number(inputPhone.value))) This worked for me in one of my projects. Also try using triple equal to instead of double if you can. Its minor but it reduces chances of bugs 1 Like |
MidasKinki: 11:39am On Aug 06, 2022 |
thug: Thanks. I have effected the changes but still not working as intended. |
MidasKinki: 11:41am On Aug 06, 2022 |
etoluw: Thank you sir. Please how do I reach your whatsapp? |
MidasKinki: 11:43am On Aug 06, 2022 |
jikins: Thanks Boss. I will try this out right away |
JsScript: 3:16pm On Aug 06, 2022 |
MidasKinki:why don't you just use input type number in your HTML, instead of trying to check if input is a number or not |
Re: HELP Javascript Devs! How Do I Re-write This If---else--if Statements To Run? by Nobody: 10:56pm On Aug 06, 2022 |
JsScript:I guess anything returned from the DOM will be of type string even if the input asks for numbers only. You’d still have to parse to int in your JS. |
MidasKinki: 11:51pm On Aug 06, 2022 |
JsScript: It's not a paid project. I just finished learning JS and tryna stretch my head. |
JsScript: 12:20am On Aug 07, 2022 |
MidasKinki:Happy stretching |
Re: HELP Javascript Devs! How Do I Re-write This If---else--if Statements To Run? by Nobody: 12:25am On Aug 07, 2022 |
MidasKinki: Try explicitly stating the data type of your inputs e.g "Number()" |
Xedmark: 10:04am On Aug 07, 2022 |
Uncle there's alot of error in your code than the problem you have. Firstly the pattern of your code is not clean and well written at all ....here's 2. There is an int that you declared as string value. Check something like "11" in your code 3. the "number" there is not suppose to be in bracket it's a string value so declare it with quotation mark. 4. Sincerely you don't need this long code to achieve this logic. U are only writing a program that checks if the input from the is empty, a number or longer than 11 character. U are actually confused by the else if, why not use switch. 1 Like |
MidasKinki: 10:19am On Aug 07, 2022 |
JsScript: Boss why na. LOL |
emetisuccess(m): 11:46am On Aug 07, 2022 |
Have u been able to solve it, if no mention me here to solve it for u.
|
Deicide: 2:04pm On Aug 07, 2022 |
MidasKinki:Where did you learn the JS from? A 2004 tutorial? |
MidasKinki: 8:35pm On Aug 07, 2022 |
Deicide: No. Why? Is the code that dirty? Code geek, please tell me how to make it better. Don't just mock me. |
Deicide: 10:13pm On Aug 07, 2022 |
MidasKinki:I am not mocking you. The last time I see people use var for JS variable it's been long. Which probably means it's an old tutorial. use the "let" or "const" keyword instead. |
MidasKinki: 11:34pm On Aug 07, 2022 |
Deicide: Thanks. I'm grateful. |