Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Been bashing my head trying to solve this practice problem:

Write a function called appendToString, which accepts two strings.

The function should return a new string which consists of the second string appended to the first string.

example: appendToString("Hello", " World!"); // "Hello World!"

What I have tried:

I got
var string = Hello, World;

function prependToString() {
  return "string"; 
}

console.log(appendToString());
Posted
Updated 3-Nov-20 20:33pm
v2

I think you need to be really careful about the names of your functions vs what you're asking for - your question with 'append' string, yet you've named your function 'prependToString', and have 'appendToString in the console write - they are two different results

So, how do you write a function in javascript ? maybe ....
function appendToString() {
}

but that's not going to work without parameters is it ?
function appendToString(base, append) {
}

ok, but it doesnt do anything nor return anything yet
function appendToString(base, append) {
    var res = base.SOMETHING(append)
    return res
}

so, how do you combine strings in javascript - look it up either in your textbook or google 'javascript combine string', and where I have put 'base.SOMETHING' above, make 'SOMETHING' the correct function .. this isnt hard, but you really need to read your textbook and apply yourself a bit
 
Share this answer
 
Hey, the code below is a function that accepts two strings in JavaScript.


let x = appendToString("Hello," , " World!"); // Hello, World!
console.log(x);

function appendToString(a, b) {
return a + b;
}

//If you want to add more strings simply add more values inside the parameters of your function //


let x = appendToString("Hello," " Beautiful " , " World!"); 
//Hello, Beautiful World! //
console.log(x);

function appendToString(a, b, c) {
return a + b + c;
}
 
Share this answer
 
v4
Comments
Richard Deeming 4-Nov-20 5:39am    
You don't help anyone by doing their homework for them. And this question was already solved back in April.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900