1

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.

9
  • 1
    Instead of logging, concatenate. Then log in the very end. Commented Nov 12, 2017 at 20:17
  • @Li357 Yes this is the first problem, the second is the message variable declaration which should be before the loop. Commented Nov 12, 2017 at 20:24
  • @chsdk var declarations are function-scoped. Though it is wrong in appearance and practice, it doesn't cause an error. Commented Nov 12, 2017 at 20:25
  • @Li357 Actually it's declared inside the loop, so each iteration will have its own message, see my answer below. Commented Nov 12, 2017 at 20:26
  • @chsdk Not really. Sure, message will get reassigned each iteration, but there is only one message in function because it's var is function-scoped. At this point it's semantics, no need to argue about that. Commented Nov 12, 2017 at 20:28

6 Answers 6

1

The message string must be defined before the loop, otherwise you don't concatenate strings, but you just override them:

function laugh(num)
{
    var message = '';

    for (var i = 0; i < num ; i += 1)
        message += "ha";

    return message + "!";
}

Debugging with console.log() has the drawback to append a line break, as other users pointed out, but the problem was not caused by this.

I also modified the loop, because declaring i as 1 and message as '' in the beginning would have produced one laugh less than expected! So either you go for:

var message = 'ha';
for (var i = 1; i < num ; i += 1)

Or you go for:

var message = '';
for (var i = 0; i < num ; i += 1)

But the first method would produce misleading results if someone applies a common logic to parametrize the function.

Sign up to request clarification or add additional context in comments.

Comments

1

console.log() appends a new line at the end.
Declare message before the for and store each part into.
Then after the loop, output it at the end with console.log() :

function laugh(num) {
    var message = "";
    for(var i = 1; i < num ; i += 1) {
        message += "ha";
    }
    console.log(message);
    return message + "!";
}

console.log(laugh(3));

Comments

1

move initial message outisde of the loop.

function laugh(num) {
    var message = "";
    for(var i = 1; i < num ; i += 1) {
        message += "ha";
    }
        return message + "!";
}

console.log(laugh(5));

below loop just assigns num times 'ha to message variable. at the end of loop it just becomes, "ha"+"!" => "ha!". and the function that wraps this loop does not finalize as you expect. the console.logs just fakes you -))

for(var i = 1; i < num ; i += 1) {
        var message = "ha";
}

Comments

1

If you prefer for loop approach to this task, you should declare message variable as an empty string outside for loop and then, with every cycle concatenate ha to the message variable.

function laugh(num) {
  var message = '';
  for (var i = 0; i < num; i += 1) {
    message += "ha";
  }
  return message + "!";
}

console.log(laugh(3));

However, if you want an easier solution and modern one, you should consider repeat function:

const smile = (num) => 'ha'.repeat(num) + '!';

console.log(smile(3));

Comments

1

You got this output because every console.log() call will be printed in a new line, and you need to declare your message variable outside the loop.

And you need to form the whole message before logging it.

function laugh(num) {
  var message = 'ha';
  for (var i = 1; i < num; i += 1) {
    message += "ha";
  }
  return message + "!";
}

Demo:

function laugh(num) {
  var message = '';
  for (var i = 1; i < num; i += 1) {
    message += "ha";
  }
  return message + "!";
}

console.log(laugh(5));

Comments

0
function laugh(num){
  var message = '' ; 
  for(var i=0;i<num;i++) {
    message= message + "ha";
  } 
  return message + "!";
}

console.log(laugh(3));

You do not need the console.log inside your function if you are going to log it outside of the function. Additionally, set a variable outside your for loop and use it to accumulate the message output.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.