2018-09-30 23:01:58 +01:00
---
id: 56533eb9ac21ba0edf2244b9
title: Constructing Strings with Variables
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/cqk8rf4'
2019-07-31 11:32:23 -07:00
forumTopicId: 16805
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
Sometimes you will need to build a string, [Mad Libs ](https://en.wikipedia.org/wiki/Mad_Libs ) style. By using the concatenation operator (`+` ), you can insert one or more variables into a string you're building.
2020-03-25 08:07:13 -07:00
Example:
```js
var ourName = "freeCodeCamp";
var ourStr = "Hello, our name is " + ourName + ", how are you?";
// ourStr is now "Hello, our name is freeCodeCamp, how are you?"
```
2020-11-27 19:02:05 +01:00
# --instructions--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
Set `myName` to a string equal to your name and build `myStr` with `myName` between the strings `"My name is "` and `" and I am well!"`
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`myName` should be set to a string at least 3 characters long.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(typeof myName !== 'undefined' & & myName.length > 2);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
You should use two `+` operators to build `myStr` with `myName` inside it.
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert(code.match(/["']\s*\+\s*myName\s*\+\s*["']/g).length > 0);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --after-user-code--
2018-09-30 23:01:58 +01:00
```js
2018-10-20 21:02:47 +03:00
(function(){
var output = [];
if(typeof myName === 'string') {
output.push('myName = "' + myName + '"');
} else {
output.push('myName is not a string');
}
if(typeof myStr === 'string') {
output.push('myStr = "' + myStr + '"');
} else {
output.push('myStr is not a string');
}
return output.join('\n');
})();
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
// Only change code below this line
var myName;
var myStr;
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
var myName = "Bob";
var myStr = "My name is " + myName + " and I am well!";
```