Files
.github
api-server
client
config
curriculum
challenges
en
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
access-array-data-with-indexes.en.md
access-multi-dimensional-arrays-with-indexes.en.md
accessing-nested-arrays.en.md
accessing-nested-objects.en.md
accessing-object-properties-with-bracket-notation.en.md
accessing-object-properties-with-dot-notation.en.md
accessing-object-properties-with-variables.en.md
add-new-properties-to-a-javascript-object.en.md
add-two-numbers-with-javascript.en.md
adding-a-default-option-in-switch-statements.en.md
appending-variables-to-strings.en.md
assignment-with-a-returned-value.en.md
build-javascript-objects.en.md
chaining-if-else-statements.en.md
comment-your-javascript-code.en.md
comparison-with-the-equality-operator.en.md
comparison-with-the-greater-than-operator.en.md
comparison-with-the-greater-than-or-equal-to-operator.en.md
comparison-with-the-inequality-operator.en.md
comparison-with-the-less-than-operator.en.md
comparison-with-the-less-than-or-equal-to-operator.en.md
comparison-with-the-strict-equality-operator.en.md
comparison-with-the-strict-inequality-operator.en.md
comparisons-with-the-logical-and-operator.en.md
comparisons-with-the-logical-or-operator.en.md
compound-assignment-with-augmented-addition.en.md
compound-assignment-with-augmented-division.en.md
compound-assignment-with-augmented-multiplication.en.md
compound-assignment-with-augmented-subtraction.en.md
concatenating-strings-with-plus-operator.en.md
concatenating-strings-with-the-plus-equals-operator.en.md
constructing-strings-with-variables.en.md
count-backwards-with-a-for-loop.en.md
counting-cards.en.md
create-decimal-numbers-with-javascript.en.md
declare-javascript-variables.en.md
declare-string-variables.en.md
decrement-a-number-with-javascript.en.md
delete-properties-from-a-javascript-object.en.md
divide-one-decimal-by-another-with-javascript.en.md
divide-one-number-by-another-with-javascript.en.md
escape-sequences-in-strings.en.md
escaping-literal-quotes-in-strings.en.md
find-the-length-of-a-string.en.md
finding-a-remainder-in-javascript.en.md
generate-random-fractions-with-javascript.en.md
generate-random-whole-numbers-with-javascript.en.md
generate-random-whole-numbers-within-a-range.en.md
global-scope-and-functions.en.md
global-vs.-local-scope-in-functions.en.md
golf-code.en.md
increment-a-number-with-javascript.en.md
initializing-variables-with-the-assignment-operator.en.md
introducing-else-if-statements.en.md
introducing-else-statements.en.md
iterate-odd-numbers-with-a-for-loop.en.md
iterate-through-an-array-with-a-for-loop.en.md
iterate-with-javascript-do...while-loops.en.md
iterate-with-javascript-for-loops.en.md
iterate-with-javascript-while-loops.en.md
local-scope-and-functions.en.md
logical-order-in-if-else-statements.en.md
manipulate-arrays-with-pop.en.md
manipulate-arrays-with-push.en.md
manipulate-arrays-with-shift.en.md
manipulate-arrays-with-unshift.en.md
manipulating-complex-objects.en.md
meta.json
modify-array-data-with-indexes.en.md
multiple-identical-options-in-switch-statements.en.md
multiply-two-decimals-with-javascript.en.md
multiply-two-numbers-with-javascript.en.md
nest-one-array-within-another-array.en.md
nesting-for-loops.en.md
passing-values-to-functions-with-arguments.en.md
practice-comparing-different-values.en.md
profile-lookup.en.md
quoting-strings-with-single-quotes.en.md
record-collection.en.md
replacing-if-else-chains-with-switch.en.md
return-a-value-from-a-function-with-return.en.md
return-early-pattern-for-functions.en.md
returning-boolean-values-from-functions.en.md
selecting-from-many-options-with-switch-statements.en.md
shopping-list.en.md
stand-in-line.en.md
store-multiple-values-in-one-variable-using-javascript-arrays.en.md
storing-values-with-the-assignment-operator.en.md
subtract-one-number-from-another-with-javascript.en.md
testing-objects-for-properties.en.md
understand-string-immutability.en.md
understanding-boolean-values.en.md
understanding-case-sensitivity-in-variables.en.md
understanding-undefined-value-returned-from-a-function.en.md
understanding-uninitialized-variables.en.md
updating-object-properties.en.md
use-bracket-notation-to-find-the-first-character-in-a-string.en.md
use-bracket-notation-to-find-the-last-character-in-a-string.en.md
use-bracket-notation-to-find-the-nth-character-in-a-string.en.md
use-bracket-notation-to-find-the-nth-to-last-character-in-a-string.en.md
use-conditional-logic-with-if-statements.en.md
use-multiple-conditional-ternary-operators.en.md
use-the-conditional-ternary-operator.en.md
use-the-parseint-function-with-a-radix.en.md
use-the-parseint-function.en.md
using-objects-for-lookups.en.md
word-blanks.en.md
write-reusable-javascript-with-functions.en.md
debugging
es6
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
basic-algorithm-scripting.json
basic-data-structures.json
basic-javascript.json
debugging.json
es6.json
functional-programming.json
intermediate-algorithm-scripting.json
javascript-algorithms-and-data-structures-projects.json
object-oriented-programming.json
regular-expressions.json
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-information-security-and-quality-assurance
08-coding-interview-prep
09-certificates
docs
formattingConversion
math-challenges
requiresTests
schema
.babelrc
.editorconfig
.eslintignore
.eslintrc
.npmignore
.prettierrc
.travis.yml
CHANGELOG.md
LICENSE.md
README.md
addAssertsToTapTest.js
challengeTitles.js
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.js
gulpfile.js
index.js
md-conversion.js
mongoIds.js
package-entry.js
package-lock.json
package.json
repack.js
test-challenges.js
unpack.js
unpacked.css
unpacked.js
unpackedChallenge.js
utils.js
docs
news
seed
tools
.editorconfig
.eslintignore
.eslintrc
.gitattributes
.gitignore
.node-inspectorrc
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE.md
README.md
docker-compose-shared.yml
docker-compose.yml
lerna.json
netlify.toml
package-lock.json
package.json
sample.env
freeCodeCamp/curriculum/challenges/en/02-javascript-algorithms-and-data-structures/basic-javascript/concatenating-strings-with-the-plus-equals-operator.en.md

75 lines
2.0 KiB
Markdown
Raw Normal View History

---
id: 56533eb9ac21ba0edf2244b8
title: Concatenating Strings with the Plus Equals Operator
challengeType: 1
---
## Description
<section id='description'>
We can also use the <code>+=</code> operator to <dfn>concatenate</dfn> a string onto the end of an existing string variable. This can be very helpful to break a long string over several lines.
<strong>Note</strong><br>Watch out for spaces. Concatenation does not add spaces between concatenated strings, so you'll need to add them yourself.
</section>
## Instructions
<section id='instructions'>
Build <code>myStr</code> over several lines by concatenating these two strings: <code>"This is the first sentence. "</code> and <code>"This is the second sentence."</code> using the <code>+=</code> operator. Use the <code>+=</code> operator similar to how it is shown in the editor. Start by assigning the first string to <code>myStr</code>, then add on the second string.
</section>
## Tests
<section id='tests'>
```yml
- text: <code>myStr</code> should have a value of <code>This is the first sentence. This is the second sentence.</code>
testString: 'assert(myStr === "This is the first sentence. This is the second sentence.", ''<code>myStr</code> should have a value of <code>This is the first sentence. This is the second sentence.</code>'');'
- text: Use the <code>+=</code> operator to build <code>myStr</code>
testString: 'assert(code.match(/\w\s*\+=\s*["'']/g).length > 1 && code.match(/\w\s*\=\s*["'']/g).length > 1, ''Use the <code>+=</code> operator to build <code>myStr</code>'');'
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
// Example
var ourStr = "I come first. ";
ourStr += "I come second.";
// Only change code below this line
var myStr;
```
</div>
### After Test
<div id='js-teardown'>
```js
console.info('after the test');
```
</div>
</section>
## Solution
<section id='solution'>
```js
var ourStr = "I come first. ";
ourStr += "I come second.";
var myStr = "This is the first sentence. ";
myStr += "This is the second sentence.";
```
</section>