javascript


This is about passing two functions as parameters and adding them to get the wanted result.

<!DOCTYPE html>
<html>
<body>



<script>

function dilver(dum,add)

{

function dum(a,b)
{
var a= 12;
var b =15;


}
function add(c,d)
{
var c = 50;
var d = 100;

}
return dum() + add();
}
var hunter = dilver(dum,add);

document.write(hunter);


</script>

</body>

</html>


When I run the code nothing happens. What am I missing ?
Last edited on
Do functions inside of functions even work? Anyways..you never return from dum or add.
http://www.w3schools.com/js/js_functions.asp

*edit after doing some reading it looks like javascript lets you embed functions.
Last edited on
What am I missing ?

A js forum to to post your question on..?
I already read it . You do not understand my question and yes it does return dum and add . YOU read this

http://stackoverflow.com/questions/13286233/javascript-pass-function-as-parameter . Think twice before posting an answer to me

The problem is that i did not find an information about how to return the sum of the values of both functions passed as a parameters in the outer function

is not this forum made to help people who have problem with some code in a

different programming language !? So why the hell do you write me this
nonsense: "What am I missing ?

A js forum to to post your question on..? " if I am in the wrong site or I made a mistake or I break the rules then feel free to correct me in an instructive way . Otherwise save your breath and do not throw empty words that has no essence
If you would break your arm, will you go to the hospital or the paint shop? Why?
If you will need help with some programming excercise will you go to the forum devoted to needed programming lnguage, filled with experts on it, or you will go to forum, where majority of people do not have knowledge of that programming language or have very basic one? Why?

yes it does return dum and add
Nobody told about not returning add or something. You have been told that you never return from add.
I'll make it easier for you. This is your add function:
1
2
3
4
5
function add(c,d)
{
    var c = 50;
    var d = 100;
}
Where does it returns and what result of execution will be?

Sorry , I misunderstand him , he is right and you are right add and dum do not return .

if we take the script like this

<script>

function dilver(dum,add)

{

function dum(a,b)
{
var a= 12;
var b =15;

return var d = a+b;


}
function add(c,d)
{
var c = 50;
var d = 100;

return var l = c+d;

}
return dum() + add();
}
var hunter = dilver(d,l);

document.write(hunter);



first is this little piece of code right or wrong "return dum() + add();"?

I need to take both result of both functions add them , then pass them to the outer function then print out the result. but I am stuck and do not know how
Last edited on
Nothing is right about any of this code. You misunderstand the basics of variables, scope, and functions. You should do a quick tutorial on that stuff first.

"Nothing is right about any of this code"

ok, then show me all the errors in this code


so that I correct them and learn from them.
Last edited on
In order to use a variable, it has to be visible inside the scope you are using it in.

When you pass a variable to a function, you don't need to have another declaration of a variable of the same name in the function.
Last edited on
ok thank you very much . Now it works better like that:

<!DOCTYPE html>
<html>
<body>



<script>

var a ;
var b ;

var c ;
var d;
var e;
var f ;

function dilver(dum,add)

{

function dum(a,b)
{
a= 12;
b =15;

return f= a+b;

}

function add(c,d)
{
c = 50;
d = 100;

return e= c+d;


}
document.write(dum());
document.write('<br/>');
document.write(add());

}
var hunter = dilver();

document.write(hunter);


</script>

</body>

</html>

but there is something weird :

the output is this :


27
150undefined



why I did not get 150 only why I got 150undefined? and how to correct it and avoid it ?
Last edited on
dilver wrote:
ok, then show me all the errors in this code

Sure.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function dilver(dum,add) //dilver accepts two parameters, dum, add
{

        function dum(a,b)  // first error, dum is a parameter, but redefined here
        {
                var a= 12; // second error, a is parameter, but redefined here  
                var b =15; // third error, b is parameter, but redefined here

                return var d = a+b; // forth error, cannot declare variable in return statement
        }
        function add(c,d) // fifth error, add is a parameter, but redefined here
        {
                var c = 50; // sixth error, c is a parameter, but redefined here
                var d = 100; // seventh error, d is a parameter, but redefined here

                return var l = c+d; // eighth error, cannot declare variable in return statement

        }
        return dum() + add(); // would normally work, but error 4 and 8 would break compilation, and thus will not work
}

var hunter = dilver(d,l); // ninth error, d and l are out of scope, not to mention not correctly declared

document.write(hunter); // would normally work, but given all other errors, will not.

gilbit wrote:
http://www.w3schools.com/js/js_functions.asp
dilver wrote:
I already read it

I'd recommend you start over on those tutorials, from the beginning. You may have read it, but from your code I'd say you didn't learn what it had to teach.

edit: by "would normally work" I do not mean work as expected.
Also, don't let us discourage you. The way I see it, all mistakes are opportunities to learn. Keep at it, it's only a matter of time before you get it. :)
Last edited on
Didn't see your reply. You're getting "150" and "undefined" on the same line because you don't put a line break between

1
2
3
var hunter = dilver();

document.write(hunter);


You're getting undefined because hunter has no return type/value and you are printing it.
Last edited on
thank you Cheraphy

I corrected the errors you mentioned now the script works except that the output is :

27
150undefined


here is the script :


<!DOCTYPE html>
<html>
<body>



<script>

var a ;
var b ;

var c ;
var d;
var e;
var f ;

function dilver(dum,add)

{

function dum(a,b)
{
a= 12;
b =15;

return f= a+b;

}

function add(c,d)
{
c = 50;
d = 100;

return e= c+d;


}
document.write(dum());
document.write('<br/>');
document.write(add());

}
var hunter = dilver();

document.write(hunter);


</script>

</body>

</html>

what could be the source of the "150undefined" instead of "150" and how to avoid it and correct it?????
I answer the first part of that question in a second post just before you made this one.

Two things need to happen so you don't get undefined on the same line, and don't get it at all.

First, you need to add a line break, like so:

1
2
3
var hunter = dilver();
document.write('</br>');
document.write(hunter);


This will change the output in question to

150
undefined


The reason you're getting undefined is because of the line:
 
document.write(hunter);


while the nested functions inside dilver have return values, dilver it's self does not. If you just want it to write

27
150



remove the line:

document.write(hunter);
Last edited on
Sorry I did not see the second post . I can say that I am amazed and many thanks for you . I understand very well and I appreciate your help

many thanks and best regards
Part of the problem is that for some reason you want to pass a function as a parameter to a function that already has that function inside it's scope. It doesn't make any sense.

You can just do this,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function f() {

    function g() {
        return 3;
    }

    function h() {
        return 2;
    }

    return g() + h();
}

f();



or if you want to pass functions as parameters,


1
2
3
4
5
6
7
8
9
10
11
12
13
function g() {
    return 3;
}

function h() {
    return 2;
}

function f( f1, f2 ) {
    return f1() + f2();
}

f( g, h );





Hey guys,
I really need your help. I'm supposed to soft-boil an egg and I don't know if:
1. i put the egg in before i heat the water?
2. i put the egg in when the water's boiling?
3. is it 5 minutes or 6?

Please, this is urgent.


(p.s. I can't use vectors)
Why don't you just microwave it?
It might eggsplode.
Topic archived. No new replies allowed.