hisoka Posted February 20, 2015 Share Posted February 20, 2015 I would like to know if it is possible in javascript to pass an if condition as a parameter function for example : function sum (alert(if(a==5) ? "good" : "wrong")) { var c = 3 ; var b = 2; var a = c+b; return a ; } Link to comment Share on other sites More sharing options...
Ingolme Posted February 20, 2015 Share Posted February 20, 2015 Perhaps this is what you're trying to do: function sum () { var c = 3 ; var b = 2; var a = c+b; if(a==5) { alert("good"); } else { alert("wrong"); }} Or this: function sum(b, c) { return b + c;}var a = sum(3, 2);if(a == 5) { alert("good");} else { alert("wrong");} Link to comment Share on other sites More sharing options...
davej Posted February 20, 2015 Share Posted February 20, 2015 Or maybe... <!DOCTYPE html><html><body><script>function sum(total,b,c) { var a = c+b; alert((a==total) ? "good" : "wrong") return a;}alert(sum(4,3,2));alert(sum(5,3,2));alert(sum(6,3,2));</script></body></html> Or even... <!DOCTYPE html><html><body><script>window.onerror = function(a, b, c, d){alert('Javascript Error:n'+a+'nURL: '+b+'nLine: '+c+' Column: '+d);return true;}</script><script>function math(guess,b,c,oper,mathf) { var ans = mathf(b,c); alert( b + oper + c + "=" + guess + " is " + ((guess==ans) ? "correct":"wrong") );}math(4,3,2,"+",function(x,y){return x+y;});math(5,3,2,"-",function(x,y){return x-y;});math(6,3,2,"/",function(x,y){return x/y;});</script></body></html> Link to comment Share on other sites More sharing options...
thescientist Posted February 20, 2015 Share Posted February 20, 2015 As suggested in another post of your, I would highly recommend reading up on the function related tutorials http://www.w3schools.com/js/js_functions.asp Link to comment Share on other sites More sharing options...
hisoka Posted February 20, 2015 Author Share Posted February 20, 2015 "I would like to know if it is possible in javascript to pass an if condition as a parameter function" generally , is it possible or it is not possible this is what I wanted to know . Nothing more nothing less Link to comment Share on other sites More sharing options...
thescientist Posted February 20, 2015 Share Posted February 20, 2015 no. functions take arguments / parameters. (like the references explain) 1 Link to comment Share on other sites More sharing options...
hisoka Posted February 21, 2015 Author Share Posted February 21, 2015 thank you Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now