+ 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
34 Respostas
+ 18
No problem, i left most of your code to make it as readable as possible, but try to make it shorter! ^^
+ 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
+ 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
+ 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! ^^
+ 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
+ 6
yeah they can have multiple if else statements
+ 6
You CAN it does not violate any "law of Javascript"
+ 6
yes they can, an even have nested if statements
+ 6
Of course x3
+ 5
Yep
+ 3
As always, thanks bro
+ 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
+ 3
I'm trying to understand that code above when I can it be more easiest
EDIT: UNDERSTOOD!
+ 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
+ 3
Wow! Interesting code now i try to understand how it choose sum or rest in that case
+ 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
+ 3
yes u can have
+ 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!).
+ 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.
+ 2
Thanks all of you for reach a bit of time to answer my question :)