Concatenate Output - Beginner Functions

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.

6 answers

  • answered 2017-11-12 20:17 Tommaso Belluzzo

    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.

  • answered 2017-11-12 20:17 chsdk

    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));

  • answered 2017-11-12 20:17 davidxxx

    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));
    

  • answered 2017-11-12 20:18 marmeladze

    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";
    }
    

  • answered 2017-11-12 20:18 Konrad D

    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));

  • answered 2017-11-12 20:29 bikeonastick

    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.