MeowHowe
MeowHowe

Reputation: 54

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.

Upvotes: 1

Views: 196

Answers (6)

bikeonastick
bikeonastick

Reputation: 789

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.

Upvotes: 0

cнŝdk
cнŝdk

Reputation: 32175

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

Upvotes: 1

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23695

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.

Upvotes: 1

kind user
kind user

Reputation: 41913

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

Upvotes: 1

marmeladze
marmeladze

Reputation: 6572

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

Upvotes: 1

davidxxx
davidxxx

Reputation: 131526

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

Upvotes: 1

Related Questions