This is a very basic function, since I just started to program in JavaScript.
function laugh(num) {
for(var i = 1; i < num ; i += 1) {
var message = "ha";
console.log(message);
}
return message + "!";
}
console.log(laugh(3));
The output I am getting is:
ha
ha
ha!
But I need it to be
hahaha!
I have tried many different strings like trying to use and empty string. But nothing seems to work.
messagevariable declaration which should be before the loop.vardeclarations are function-scoped. Though it is wrong in appearance and practice, it doesn't cause an error.message, see my answer below.messagewill get reassigned each iteration, but there is only onemessagein function because it'svaris function-scoped. At this point it's semantics, no need to argue about that.