Files
.github
api-server
client
config
curriculum
__fixtures__
challenges
_meta
arabic
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
basic-javascript-rpg-game
debugging
es6
functional-programming
functional-programming-spreadsheet
intermediate-algorithm-scripting
intermediate-javascript-calorie-counter
part-001.md
part-002.md
part-003.md
part-004.md
part-005.md
part-006.md
part-007.md
part-008.md
part-009.md
part-010.md
part-011.md
part-012.md
part-013.md
part-014.md
part-015.md
part-016.md
part-017.md
part-018.md
part-019.md
part-020.md
part-021.md
part-022.md
part-023.md
part-024.md
part-025.md
part-026.md
part-027.md
part-028.md
part-029.md
part-030.md
part-031.md
part-032.md
part-033.md
part-034.md
part-035.md
part-036.md
part-037.md
part-038.md
part-039.md
part-040.md
part-041.md
part-042.md
part-043.md
part-044.md
part-045.md
part-046.md
part-047.md
part-048.md
part-049.md
part-050.md
part-051.md
part-052.md
part-053.md
part-054.md
part-055.md
part-056.md
part-057.md
part-058.md
part-059.md
part-060.md
part-061.md
part-062.md
part-063.md
part-064.md
part-065.md
part-066.md
part-067.md
part-068.md
part-069.md
part-070.md
part-071.md
part-072.md
part-073.md
part-074.md
part-075.md
part-076.md
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-quality-assurance
07-scientific-computing-with-python
08-data-analysis-with-python
09-information-security
10-coding-interview-prep
11-machine-learning-with-python
12-certificates
portuguese
russian
spanish
schema
test
.babelrc
LICENSE.md
comment-dictionary.js
create-challenge-bundle.js
getChallenges.acceptance.test.js
getChallenges.js
getChallenges.test.js
gulpfile.js
lib.js
package-entry.js
package-lock.json
package.json
utils.js
cypress
docs
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.npmrc
.prettierignore
.prettierrc
.snyk
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
HoF.md
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
crowdin.yml
cypress-install.js
cypress.json
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
jest.config.js
lerna.json
lighthouserc.js
package-lock.json
package.json
sample.env

153 lines
3.0 KiB
Markdown
Raw Normal View History

2019-12-07 17:11:14 -07:00
---
id: 5ddb965c65d27e1512d44daa
title: Part 17
challengeType: 0
isHidden: true
2019-12-07 17:11:14 -07:00
---
## Description
<section id='description'>
While you can use a loop to add everything in the `total` array to a variable, JavaScript provides the useful `reduce()` method.
Chain the `reduce()` method to the `Array.from()` expression.
</section>
## Instructions
<section id='instructions'>
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: See description above for instructions.
testString: assert( code.replace(/\s/g, '').match(/Number\(meal\.value\)\)\.reduce\(\)/) );
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='html-seed'>
```html
<script>
document.getElementById('calorie-form').onsubmit = calculate;
function calculate(e) {
e.preventDefault();
const total = Array.from(
document.getElementsByClassName('cal-control')
).map(meal => Number(meal.value));
}
</script>
```
</div>
### Before Test
<div id='html-setup'>
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<meta name="description" content="" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div class="container">
<form id="calorie-form">
<h2 class="center">Calorie Counter</h2>
<div class="grid">
<legend>Sex</legend>
<div>
<input type="radio" name="sex" id="female" value="F" checked />
<label for="female">
Female (2,000 calories)
</label>
<div>
<input type="radio" name="sex" id="male" value="M" />
<label for="male">
Male (2,500 calories)
</label>
</div>
</div>
</div>
<div class="grid" id="entries">
Breakfast
<input
type="number"
min="0"
class="cal-control"
id="breakfast"
/><br />
Lunch
<input type="number" min="0" class="cal-control" id="lunch" /><br />
Dinner <input type="number" min="0" class="cal-control" id="dinner" />
</div>
<button type="button" class="btn-add" id="add">
Add Entry
</button>
<button type="submit" class="btn-solid" id="calculate">
Calculate
</button>
<button type="button" class="btn-outline" id="clear">
Clear
</button>
</form>
<div id="output"></div>
</div>
</body>
</html>
```
</div>
### After Test
<div id='html-teardown'>
```html
</body>
</html>
```
</div>
</section>
## Solution
<section id='solution'>
```html
<script>
document.getElementById('calorie-form').onsubmit = calculate;
function calculate(e) {
e.preventDefault();
const total = Array.from(document.getElementsByClassName('cal-control'))
.map(meal => Number(meal.value))
.reduce();
}
</script>
```
</section>