0

Why does the code print undefined?

function gainAcess(gender,age) { if ((gender=="male") && (age>=18) || (gender=="female") && (age>=18)) { document.write("Access is gained"); }else if((gender=="male") && (age<18) ||(gender=="female") && (age<18)){ document.write("Access is lost");} } document.write(gainAcess("female",24));

8th Aug 2017, 7:55 PM
Manik Tafralian
Manik Tafralian - avatar
2 odpowiedzi
+ 6
Because the function doesn't return anything. If you want to write the output, return that output instead of writing it -- otherwise just call it instead of writing the result of the call. (too many writes, cut out one side): function ... { writes } gainAcess(...) ----- or: function ... { return "what to print"; } document.write(gainAcess())
8th Aug 2017, 7:58 PM
Kirk Schafer
Kirk Schafer - avatar
+ 1
Thanks a lot.It worked with return statement :)))
8th Aug 2017, 8:53 PM
Manik Tafralian
Manik Tafralian - avatar