Need help in creating function to concatenation

Var hello= “I love js”;
Var num = 6;

Function concatenation (hello, num){
Return hello.concate(num);
}

Console.log(concatenation);

I dnt know where i am getting error please help

First, javascript is a case sensitive language. This means that identifiers like ‘var’, ‘function’, ‘return’, etc. all are lower case identifiers. Using ‘Var’ will not work, nor will ‘Function’.

Second, review the way to define a function. Your declaration would be fine, but the hello and num are confusing in this context since you have declared variables above that declaration with the same name. Think of the variables in the function declaration as a placeholder variable that describes the variable you expect to be passed as an argument to the function. For example:

function concatenation(word1, word2) {
//code here
}

You can then use word1 and word2 within the body of the function to carry out the work.

Lastly, after creating a function that has two arguments, you must pass those values in when calling the function.

console.log(concatenation(hello, num));

I have fixed all of the errors below if the above tips don’t help you get it to the finish line. Good luck!

Hint
var hello= "I love js";
var num = 6;

function concatenation (word1, word2){
return word1.concat(word2);
}
console.log(concatenation(hello, num));
1 Like

Wow. Thank you so much. It was very helpful. I have implemented what you taught. Thankyou

I have improved my few codes can you please review it

var hello= “I love js”;
var num = 6;

function concatenation (word1, word2){
return word1.concat(word2);
}
console.log(concatenation(hello, num));

function upper (word){
return hello.toUpperCase();
}
console.log(upper(hello));

function convertToArray (word3){
return hello.split()
}
console.log(convertToArray(hello));

console.log(convertToArray(hello.reverse()));

I wonder if we could convert from array to string?
If yes how can we do that?

Remember that inside the function body you should be using the name of the parameter. In your other functions you are actually using the globally scoped variable ‘hello’ instead of the parameter. This will cause unexpected results.
For example:

// your current version
function upper (word){
   return hello.toUpperCase(); // here you are calling toUpperCase() 
                               // on the global variable hello
}

Instead you should be using the name of the parameter(s) in the function definition. The parameter servers as the container for variables that are passed to the function in the function call.

function upper (word){
   return word.toUpperCase(); //notice the variable word, this is the 
                              // parameter we defined for this function. 
}

As for converting an array to string review the following Array function: .join()

1 Like