laugh()
does not explicitly return a value; therefore its return type is undefined
. You're calling laugh()
from within a console.log()
- the result being that you see all the console.log()
calls that occur within laugh()
then, additionally, the undefined
being returned by laugh()
is being logged to the console.
You could return a string if you want to log the result of the function. Something like this:
var laugh = function laughs(y) {
var str = '';
while (y > 0) {
//console.log("ha");
str += 'ha\n';
y--;
}
//console.log("ha!");
str += 'ha!\n';
return str;
}
console.log(laugh(10));
Alternatively, you could just remove the console.log()
enclosing the function call like this:
var laugh = function laughs(y) {
while (y > 0) {
console.log("ha");
y--;
}
console.log("ha!");
}
laugh(10);