0

why doesnt return work

function sayHello2(name) { var text = 'Hello ' + name; // Local variable var say = function() { console.log(text); } return say; } var say2 = sayHello2('Bob'); say2(); // logs "Hello Bob" document.write(say2); // Output: function() { console.log(text); }

18th May 2018, 2:09 PM
IZzZI
IZzZI - avatar
6 Antworten
0
hmmm idk. when i run the code it ouputs as: function() { console.log(text); }
18th May 2018, 2:25 PM
IZzZI
IZzZI - avatar
0
I expected the output to be: Hello Bob
18th May 2018, 2:33 PM
IZzZI
IZzZI - avatar
0
You have to give document.write() something that can be written to the page. At the moment you are giving it a function. You might have wanted to write document.write(say2()) but this also wouldn’t work because say2 doesn‘t return anything
18th May 2018, 2:36 PM
Max
Max - avatar
0
function sayHello3(name) { var text = 'Hello ' + name; // Local variable var say = function() { console.log(text); return text return say; } var say3 = sayHello3('Joe'); var z = say3(); document.write(z); Finally worked to a solution
19th May 2018, 12:29 PM
IZzZI
IZzZI - avatar
- 1
What’s the problem? Seems to work as intended
18th May 2018, 2:22 PM
Max
Max - avatar
- 2
What do you expect/want the output to be?
18th May 2018, 2:26 PM
Max
Max - avatar