+ 11

Can a function have an if statement?

I'm trying to make an if on it but i can't resolve the problem. Can you help me? https://code.sololearn.com/WZ8wu2RATkEX/?ref=app

21st Jul 2017, 5:29 PM
Pablo Rodríguez
Pablo Rodríguez - avatar
34 odpowiedzi
+ 18
No problem, i left most of your code to make it as readable as possible, but try to make it shorter! ^^
21st Jul 2017, 7:03 PM
Maz
Maz - avatar
+ 17
Thank you Pablo, i really needed to make some practice with Javascript, this is an old method to short your code i recently meet from mr.Google, there are many ways to optimize a code, also my code has some issues yet (controls must be improved), so... let's try to solve them! ^^ https://code.sololearn.com/W44oM8l0KHQr/?ref=app
21st Jul 2017, 11:41 PM
Maz
Maz - avatar
+ 15
Pablo it uses the parameter "op" (operation). When you call the function you can pass two arguments between "sum" and "rest". Inside the function it checks what you chose and it give different outputs in base to your choose: if (op == "sum) sum = one + two; else if (op == "rest") sum = one - two; else // no other controls, try to add them
21st Jul 2017, 11:59 PM
Maz
Maz - avatar
+ 13
Pablo, one of the easiest way is check if the number is really a number or not... in addition check if the user inserts an empty value: if(one!=""&&two!=""&&!isNaN(one)&&!isNaN(two)) sum = one + two; else sum = ""; ... you can also use the ternary operator rather than the if condition: sum=one!=""&&two!=""&&!isNaN(one)&&!isNaN(two)?one+two:"" ... the code is not complete yet, after that you'll call again the "sum" variable, try to make more controls on your own! ^^
21st Jul 2017, 8:21 PM
Maz
Maz - avatar
+ 10
The problem is that the code does not work as you want, the easiest thing to do is check the value inserted by the user through a variable, in this case you can check if a number is decimal doing something like: if(one % 1!=0 || two % 1!=0) alert('Only integers'); ... but this solution does not stop the execution of the program! You can use the parseInt() method to convert the decimal numbers in integer numbers without make useless controls, check it out my solution made for your first function. But also this solution is optimizable, you have to make more controls, what if i insert a letter rather than a number? It is really needed make multiple functions to make the same thing... ? Have fun! ^^ https://code.sololearn.com/Wm18t86sX8Tc/?ref=app
21st Jul 2017, 6:59 PM
Maz
Maz - avatar
+ 6
yeah they can have multiple if else statements
21st Jul 2017, 5:36 PM
_Retr0/-
_Retr0/- - avatar
+ 6
You CAN it does not violate any "law of Javascript"
24th Jul 2017, 6:43 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 6
yes they can, an even have nested if statements
24th Jul 2017, 2:38 PM
Krafty Coder
Krafty Coder - avatar
+ 6
Of course x3
25th Jul 2017, 8:43 PM
Moon
Moon - avatar
+ 5
Yep
25th Jul 2017, 7:15 PM
Iwan
Iwan - avatar
+ 3
As always, thanks bro
21st Jul 2017, 7:01 PM
Pablo Rodríguez
Pablo Rodríguez - avatar
+ 3
the code is optimized and only permit integers, now the worst part, not permit strings as an input :D https://code.sololearn.com/WZ8wu2RATkEX/?ref=app
21st Jul 2017, 8:00 PM
Pablo Rodríguez
Pablo Rodríguez - avatar
+ 3
I'm trying to understand that code above when I can it be more easiest EDIT: UNDERSTOOD!
21st Jul 2017, 8:39 PM
Pablo Rodríguez
Pablo Rodríguez - avatar
+ 3
i resolve it as simply as hell... i can't found how recall the prompt again without the "DRY" i put a default valor of 0 if you enter a letter or let the box blank so that's it https://code.sololearn.com/WZ8wu2RATkEX/?ref=app
21st Jul 2017, 10:21 PM
Pablo Rodríguez
Pablo Rodríguez - avatar
+ 3
Wow! Interesting code now i try to understand how it choose sum or rest in that case
21st Jul 2017, 11:54 PM
Pablo Rodríguez
Pablo Rodríguez - avatar
+ 3
It made first the sum and then the rest (in your code) it will be a lot of fun if we add a selector where we can choose sum or rest. The simplicity of the code is awesome. i can restructure my code for insert this method
22nd Jul 2017, 12:11 AM
Pablo Rodríguez
Pablo Rodríguez - avatar
+ 3
yes u can have
22nd Jul 2017, 4:01 AM
KABILESH KANNAN
KABILESH KANNAN - avatar
+ 3
I had an issue with 0's. They fail because of == vs. ===: 0 == "" // true 0 != "" // false 0 === "" // false 0 !== "" // true However...parseFloat returns Numeric NaN on empty strings (I don't see how "" would get through; but I'm tired!).
23rd Jul 2017, 5:34 AM
Kirk Schafer
Kirk Schafer - avatar
+ 2
You need to include curly braces in your if statement! The curly braces surround the code that is to be executed, if the condition is true.
22nd Jul 2017, 2:42 PM
Erik Johanson
Erik Johanson - avatar
+ 2
Thanks all of you for reach a bit of time to answer my question :)
23rd Jul 2017, 11:16 PM
Pablo Rodríguez
Pablo Rodríguez - avatar