-
-
Notifications
You must be signed in to change notification settings - Fork 637
Concept exercise: Template string and Ternary #1428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e65eb63
a75999d
5b18ad7
6e49c27
db66f01
f1b052d
98cb08c
abd8ca5
1ae9d16
3c74b86
56fc918
822b7ad
3387c31
150404a
cbc5260
4f094ff
da9a260
fcf1fe8
1b3069d
a871a1b
e961b98
eb88316
82a4763
0d76eaf
47333f2
a3186c0
f5b68e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"blurb": "The conditional ternary operator is used to write a condensed expression that returns one of two alternate values based on some condition.", | ||
"authors": ["pertrai1"], | ||
"contributors": [] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# About | ||
|
||
The conditional ternary operator is used to write a condensed expression that returns one of two alternate values based on some condition. | ||
It is often referred to as the "ternary operator". | ||
The name stems from the fact that the operator has three operands: `predicate ? consequent-expression : alternative-expression` | ||
It can be used as a replacement for short if-else statements. | ||
|
||
Similar to `if` statements, JavaScript will perform implicit type conversion to evaluate the condition. | ||
If the condition is truthy, the operand on the left-hand side of the colon will be returned. | ||
Otherwise the result of the ternary expression is the operand on the right-hand side of the colon. | ||
|
||
```javascript | ||
const year = 2020; | ||
|
||
year > 2000 ? 'in the past years' : 'a long time ago'; | ||
// => 'in the past years' | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Introduction | ||
|
||
The conditional ternary operator is used to write a condensed expression that returns one of two alternate values based on some condition. | ||
It is often referred to as the "ternary operator". | ||
The name stems from the fact that the operator has three operands: `predicate ? consequent-expression : alternative-expression` | ||
It can be used as a replacement for short if-else statements. | ||
|
||
Similar to `if` statements, JavaScript will perform implicit type conversion to evaluate the condition. | ||
If the condition is truthy, the operand on the left-hand side of the colon will be returned. | ||
Otherwise the result of the ternary expression is the operand on the right-hand side of the colon. | ||
|
||
```javascript | ||
const year = 2020; | ||
|
||
year > 2000 ? 'in the past years' : 'a long time ago'; | ||
// => 'in the past years' | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Instance_methods", | ||
"description": "MDN: Conditional (ternary) operator" | ||
}, | ||
{ | ||
"url": "https://javascript.info/ifelse", | ||
"description": "javascript.info: Conditional branching" | ||
} | ||
] |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"blurb": "You can create template strings in JavaScript by wrapping text in backticks. They not only allow the text to include new lines and other special characters, you can also embed variables and other expressions.", | ||
"authors": ["pertrai1"], | ||
"contributors": [] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Introduction | ||
|
||
In JavaScript, _template strings_ allows for embedding expressions in strings, also referred to as string interpolation. | ||
This functionality extends the functionality of the built in [`String`][string-reference] global object. | ||
|
||
You can create template strings in JavaScript by wrapping text in backticks. | ||
They not only allow the text to include new lines and other special characters, you can also embed variables and other expressions. | ||
|
||
```javascript | ||
const num1 = 1; | ||
const num2 = 2; | ||
|
||
`Adding ${num1} and ${num2} gives ${num1 + num2}.`; | ||
// => Adding 1 and 2 gives 3. | ||
``` | ||
|
||
In the example above, backticks - (<code>\`\`</code>) - are used to represent a template string. The`${...}` is the placeholder that is used for substitution. | ||
Any non-string types are _implicitly_ converted to strings. | ||
This topic is covered in the [type conversion][type-conversion-concept] concept. | ||
All types of expressions can be used with template strings. | ||
|
||
```javascript | ||
const track = 'JavaScript'; | ||
|
||
`This track on exercism.io is ${track.toUpperCase()}.`; | ||
// => This track on exercism.io is JAVASCRIPT. | ||
``` | ||
|
||
When you are needing to have strings formatted on multiple lines: | ||
|
||
```javascript | ||
`This is example of using template | ||
strings to accomplish multiple | ||
lines`; | ||
``` | ||
|
||
With the substitution capabilities that are available, you can also introduce logic into the process to determine what the output string should be. | ||
One way to handle the logic could be using the [ternary operator][ternary-operator]. | ||
This gives the same conditional `if/else` functionality in a slightly different format. | ||
|
||
To implement logic into template string syntax: | ||
|
||
```javascript | ||
const grade = 95; | ||
|
||
`You have ${grade > 90 ? 'passed' : 'failed'} the exam.`; | ||
// => You have passed the exam. | ||
``` | ||
|
||
[string-reference]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String | ||
[type-conversion-concept]: /tracks/javascript/concepts/type-conversion | ||
[ternary-operator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Introduction | ||
|
||
In JavaScript, _template strings_ allows for embedding expressions in strings, also referred to as string interpolation. | ||
This functionality extends the functionality of the built in [`String`][string-reference] global object. | ||
|
||
You can create template strings in JavaScript by wrapping text in backticks. | ||
They not only allow the text to include new lines and other special characters, you can also embed variables and other expressions. | ||
|
||
junedev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
```javascript | ||
const num1 = 1; | ||
const num2 = 2; | ||
|
||
`Adding ${num1} and ${num2} gives ${num1 + num2}.`; | ||
// => Adding 1 and 2 gives 3. | ||
``` | ||
junedev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
In the example above, backticks - (<code>\`\`</code>) - are used to represent a template string. The`${...}` is the placeholder that is used for substitution. | ||
Any non-string types are _implicitly_ converted to strings. | ||
This topic is covered in the [type conversion][type-conversion-concept] concept. | ||
All types of expressions can be used with template strings. | ||
|
||
```javascript | ||
const track = 'JavaScript'; | ||
|
||
`This track on exercism.io is ${track.toUpperCase()}.`; | ||
// => This track on exercism.io is JAVASCRIPT. | ||
``` | ||
|
||
When you are needing to have strings formatted on multiple lines: | ||
|
||
```javascript | ||
`This is example of using template | ||
strings to accomplish multiple | ||
lines`; | ||
``` | ||
|
||
With the substitution capabilities that are available, you can also introduce logic into the process to determine what the output string should be. | ||
One way to handle the logic could be using the [ternary operator][ternary-operator]. | ||
This gives the same conditional `if/else` functionality in a slightly different format. | ||
|
||
To implement logic into template string syntax: | ||
|
||
```javascript | ||
const grade = 95; | ||
|
||
`You have ${grade > 90 ? 'passed' : 'failed'} the exam.`; | ||
// => You have passed the exam. | ||
``` | ||
|
||
[string-reference]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String | ||
[type-conversion-concept]: /tracks/javascript/concepts/type-conversion | ||
[ternary-operator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's missing for me is that CSS-in-JS solutions often uses the same technique. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @SleeplessByte - are you saying that we should add something that talks about tagged template literals? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals", | ||
"description": "MDN: Template literals" | ||
}, | ||
{ | ||
"url": "https://developers.google.com/web/updates/2015/01/ES6-Template-Strings", | ||
"description": "Google Developers: Getting Literal With ES6 Template Strings" | ||
} | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Hints | ||
|
||
## 1. Build an occasion sign | ||
|
||
- [template strings][mdn-template-strings] (Template literals) allow for substitution of one or more strings and embedded expressions | ||
|
||
## 2. Build a birthday sign | ||
|
||
- [template strings][mdn-template-strings] (Template literals) allow for substitution of strings and embedded expressions | ||
- [ternary operator][mdn-ternary-operator] is a short-hand way of operating on conditions, similar to `if/else`. It can be easier to use in template strings because it is condensed. | ||
|
||
## 4. Build a graduation sign | ||
pertrai1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- [template strings][mdn-template-strings] allow strings to span onto multiple lines. | ||
|
||
## 5. Compute the cost of a sign | ||
|
||
- Figure out the [`length`][mdn-string-length] of the characters. | ||
- Only show the first two decimal numbers using [fixed-point][mdn-to-fixed] notation. | ||
|
||
[mdn-const]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const | ||
[mdn-template-strings]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals | ||
[mdn-string-length]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length | ||
[mdn-to-fixed]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed | ||
[mdn-ternary-operator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Instructions | ||
|
||
In this exercise you'll be writing code to help a sign company create custom messages for their signs. | ||
|
||
## 1. Build an occasion sign | ||
|
||
Implement the function `buildSign(occasion, name)` that accepts a string as the `occasion` parameter and a string holding someone's name as the `name` parameter. The two parameters will be embedded into a template string to output the message on the sign. | ||
|
||
```javascript | ||
buildSign('Birthday', 'Rob'); | ||
// => "Happy Birthday Rob!" | ||
``` | ||
|
||
## 2. Build a birthday sign | ||
|
||
Implement the function `buildBirthdaySign(age)` that accepts an age and based on the age will determine part of the message on the sign. If the age is 50 or older, the sign will include the word _old_, otherwise the sign will include the word _young_. | ||
|
||
```javascript | ||
buildBirthdaySign(45); | ||
// => "Happy Birthday! What a young fellow you are." | ||
``` | ||
|
||
## 3. Build a graduation sign | ||
|
||
Implement the function `graduationFor(name, year)` which takes a name as a string parameter and a year as a number parameter and uses string interpolation to create a phrase for a sign that uses a newline to separate the two lines of the message. | ||
|
||
```javascript | ||
graduationFor('Hannah', 2022); | ||
// => "Congratulations Hannah!\nClass of 2022" | ||
``` | ||
|
||
## 5. Compute the cost of a sign | ||
|
||
Implement the function `costOf(sign, currency)` which takes a string that holds the contents of the sign and a string that represents the currency. | ||
The sign has a base price of 20 in the given currency. Additionally each letter costs 2. (Whitespaces are included in the calculation.) | ||
The phrase returned includes the cost to create the sign, written with two digits after the decimal point, followed by the currency string. | ||
|
||
```javascript | ||
costOf('Happy Birthday Rob!', 'dollars'); | ||
// => "Your sign costs 58.00 dollars." | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Introduction | ||
|
||
In JavaScript, _template strings_ allows for embedding expressions in strings, also referred to as string interpolation. | ||
This functionality extends the functionality of the built in [`String`][string-reference] global object. | ||
|
||
You can create template strings in JavaScript by wrapping text in backticks. | ||
They not only allow the text to include new lines and other special characters, you can also embed variables and other expressions. | ||
|
||
```javascript | ||
const num1 = 1; | ||
const num2 = 2; | ||
|
||
`Adding ${num1} and ${num2} gives ${num1 + num2}.`; | ||
// => Adding 1 and 2 gives 3. | ||
``` | ||
|
||
In the example above, backticks - (<code>\`\`</code>) - are used to represent a template string. The`${...}` is the placeholder that is used for substitution. | ||
Any non-string types are _implicitly_ converted to strings. | ||
This topic is covered in the [type conversion][type-conversion-concept] concept. | ||
All types of expressions can be used with template strings. | ||
|
||
```javascript | ||
const track = 'JavaScript'; | ||
|
||
`This track on exercism.io is ${track.toUpperCase()}.`; | ||
// => This track on exercism.io is JAVASCRIPT. | ||
``` | ||
|
||
When you are needing to have strings formatted on multiple lines: | ||
|
||
```javascript | ||
`This is example of using template | ||
strings to accomplish multiple | ||
lines`; | ||
``` | ||
|
||
With the substitution capabilities that are available, you can also introduce logic into the process to determine what the output string should be. | ||
|
||
An example for this is embedding a [ternary operator][ternary-operator]. | ||
This operator is a short form for writing an `if/else` statement. | ||
The syntax is `condition ? consequent-expression : alternative-expression`. | ||
If the condition is truthy, the operand on the left-hand side of the colon will be returned. | ||
Otherwise the result of the ternary expression is the operand on the right-hand side of the colon. | ||
|
||
```javascript | ||
const grade = 95; | ||
|
||
`You have ${grade > 90 ? 'passed' : 'failed'} the exam.`; | ||
// => You have passed the exam. | ||
``` | ||
|
||
[string-reference]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String | ||
[type-conversion-concept]: /tracks/javascript/concepts/type-conversion | ||
[ternary-operator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"root": true, | ||
"extends": "@exercism/eslint-config-javascript", | ||
"env": { | ||
"jest": true | ||
}, | ||
"overrides": [ | ||
{ | ||
"files": [".meta/proof.ci.js", ".meta/exemplar.js", "*.spec.js"], | ||
"excludedFiles": ["custom.spec.js"], | ||
"extends": "@exercism/eslint-config-javascript/maintainers" | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
yarn-error.log | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"blurb": "Learn about template strings and the ternary operator ...", | ||
"authors": ["pertrai1"], | ||
"contributors": [], | ||
"files": { | ||
"solution": ["custom-signs.js"], | ||
"test": ["custom-signs.spec.js"], | ||
"exemplar": [".meta/exemplar.js"] | ||
}, | ||
"forked_from": ["swift/custom-signs"] | ||
} |
Uh oh!
There was an error while loading. Please reload this page.