Javascript challenge issue

Hey guys and gals, im working on this challenge for a prep course and im having trouble finding what im doing wrong.

The problem is to join two strings using the concat method. The text input area was already set up with the function which doesnt make sense to me on how to use it. I was under the impression that the correct function was actually .concat ?

here is the code i have, the function that is at the top was provided already so i removed the “Your code goes here text” and left the curly braces then added my code.

function concatenateTwoStrings(string1, string2) {
  var string1 = "fresh";
  var string2 = "start";
   var output = .concat("fresh", "start");
   console.log(output);

.concat is not valid syntax. you need to use the method on something. Also you should use the given variables, not write the strings again.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

function concatenateTwoStrings(string1, string2) {
var string1 = “fresh”;
var string2 = “start”;
var output = string1.concat(string2);
console.log(output);
}

this is the correct syntax for concat and actually there is no need to create separate function for concat, concat itself in in built function

the function that is written at the top was already there when i started the challenge but i couldnt get it to work. should i change the var for output because i dont think they want me to remove the given function.

Yea i just tryed to delete the given function concatenateTwoStrings and the error read "function concatenateTwoStrings not defined. Im pretty sure it wants me to complet this challenge using the given function.

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

You want to keep what you have above but modify it to correctly use concatenation.

You need to

  1. use the variables for your strings with concat.
  2. use correct syntax for concat.

The syntax for concat is

myFirstString.concat(mySecondString)

No i get it and that is what was confusing me so much. They want me to use the function at the beginning that was given to us. I just dont know how to use it properly i guess.

What is your current code?

1 Like

This was how it looked when i started the challenge.

function concatenateTwoStrings(string1, string2) {
  // your code here
}


This is what i have currently.

//  function concatenateTwoStrings(string1, string2) 
{
      var string1 = "Fresh";
  var string2 = "Start";
  var output = concatenateTwoStrings("string1", "string2");
  console.log(output);
 
}

I also tried it where the var output was this instead but that didnt work either.

var output= string1.concat("string2");

Got it :slightly_smiling_face:

I’m not sure why you commented out the start of the function. You also are overwriting the input of the functions, which defeats the purpose of functions! I’d start from the beginning.

function concatenateTwoStrings(string1, string2) {
// your code here
}

We get two strings, string1 and string2. We want to concatenate the two and return the result.

Cool. There is a built in function for concatenation, called .concat(). Let’s look up what it does.

The documentation has this example

const str1 = 'Hello';
const str2 = 'World';

console.log(str1.concat(' ', str2));
// expected output: "Hello World"

console.log(str2.concat(', ', str1));
// expected output: "World, Hello"

What happens if we try some of this, but with our variable names instead?

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

1 Like

i understand what your saying about the function at the top of the input box, but what im telling you is that was already there! Theyre basically telling me i have to use the function they provided. Hence the title of the challenge being complete the function.

Ive tried to remove said function and just solely use .concat() and it brings up an error saying that function concatenateTwoStrings not defined

I’m also telling you to use the function provided.

You have

function concatenateTwoStrings(string1, string2) {
// your code here
}

You need to take string1 and string2 and concatenate them inside of the function and return the result.

The example provided by MDN shows how to concatenate two strings. It’s a little different than what you have. Does it make sense, or should I explain it better?

so i should be using const instead of var?

That’s not the issue here.

Think about what the challenge is asking. The challenge names a function concatenateTwoStrings which takes two inputs, string1 and string2. Your task is to write the code for the function to put string1 and string2 together.

So if I run concatenateTwoStrings("Try", "this") I get "Try this".

1 Like

nope that is not working either

this is not working, either as const or var

You should take the two input strings and return a single string with the pair concatenated. What is your current code?