Files
.github
api-server
client
config
curriculum
__fixtures__
challenges
_meta
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
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
part-077.md
part-078.md
part-079.md
part-080.md
part-081.md
part-082.md
part-083.md
part-084.md
part-085.md
part-086.md
part-087.md
part-088.md
part-089.md
part-090.md
part-091.md
part-092.md
part-093.md
part-094.md
part-095.md
part-096.md
part-097.md
part-098.md
part-099.md
part-100.md
part-101.md
part-102.md
part-103.md
part-104.md
part-105.md
part-106.md
part-107.md
part-108.md
part-109.md
part-110.md
part-111.md
part-112.md
part-113.md
part-114.md
part-115.md
part-116.md
part-117.md
part-118.md
part-119.md
part-120.md
part-121.md
part-122.md
part-123.md
part-124.md
part-125.md
part-126.md
part-127.md
part-128.md
part-129.md
part-130.md
part-131.md
part-132.md
part-133.md
part-134.md
part-135.md
part-136.md
part-137.md
part-138.md
part-139.md
part-140.md
part-141.md
intermediate-algorithm-scripting
intermediate-javascript-calorie-counter
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
dictionaries
schema
test
.babelrc
LICENSE.md
create-challenge-bundle.js
crowdin.yml
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-base.json
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.npmrc
.prettierignore
.prettierrc
.snyk
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
HoF.md
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
cypress-install.js
cypress.json
jest.config.js
lerna.json
lighthouserc.js
package-lock.json
package.json
sample.env
Randell Dawson 94f0cf0ef8 chore(learn): Remove remaining isHidden keys from frontmatter (English and Chinese challenges) ()
* fix: remove isHidden key from tool template

* fix: removed isHidden key from English challenges

* fix: remove isHidden key from Chinese challenges
2020-10-08 14:18:47 +02:00

212 lines
4.5 KiB
Markdown

---
id: 5d792536ddff9ea73c90a994
title: Part 68
challengeType: 0
---
## Description
<section id='description'>
However, you don't need an arrow function. As `elemValue(n)` is a function, you can pass it to `map` directly.
Change `x => elemValue(n)(x)` to `elemValue(n)`.
</section>
## Instructions
<section id='instructions'>
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: See description above for instructions.
testString: assert(code.replace(/\s/g, "").includes("constaddChars=c1=>c2=>n=>charRange(c1,c2).map(elemValue(n))"));
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='html-seed'>
```html
<script>
const infixToFunction = {
"+": (x, y) => x + y,
"-": (x, y) => x - y,
"*": (x, y) => x * y,
"/": (x, y) => x / y
};
const infixEval = (str, regex) =>
str.replace(regex, (_, arg1, fn, arg2) =>
infixToFunction[fn](parseFloat(arg1), parseFloat(arg2))
);
const highPrecedence = str => {
const regex = /([0-9.]+)([*\/])([0-9.]+)/;
const str2 = infixEval(str, regex);
return str === str2 ? str : highPrecedence(str2);
};
const spreadsheetFunctions = {
"": x => x
};
const applyFn = str => {
const noHigh = highPrecedence(str);
const infix = /([0-9.]+)([+-])([0-9.]+)/;
const str2 = infixEval(noHigh, infix);
const regex = /([a-z]*)\(([0-9., ]*)\)(?!.*\()/i;
const toNumberList = args => args.split(",").map(parseFloat);
const applyFunction = (fn, args) =>
spreadsheetFunctions[fn.toLowerCase()](toNumberList(args));
return str2.replace(
regex,
(match, fn, args) =>
spreadsheetFunctions.hasOwnProperty(fn.toLowerCase()) ? applyFunction(fn, args) : match
);
};
const range = (start, end) =>
start > end ? [] : [start].concat(range(start + 1, end));
const charRange = (start, end) =>
range(start.charCodeAt(0), end.charCodeAt(0)).map(x =>
String.fromCharCode(x)
);
const evalFormula = x => {
const rangeRegex = /([A-J])([1-9][0-9]?):([A-J])([1-9][0-9]?)/gi;
const rangeFromString = (n1, n2) => range(parseInt(n1), parseInt(n2));
const elemValue = n => c => document.getElementById(c + n).value;
const addChars = c1 => c2 => n => charRange(c1, c2).map(x => elemValue(n)(x));
const fn = elemValue("1");
return fn("A");
};
</script>
```
</div>
### Before Test
<div id='html-setup'>
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Spreadsheet</title>
<style>
#container {
display: grid;
grid-template-columns: 50px repeat(10, 200px);
grid-template-rows: repeat(11, 30px);
}
.label {
background-color: lightgray;
text-align: center;
vertical-align: middle;
line-height: 30px;
}
</style>
</head>
<body>
<div id="container">
<div></div>
</div>
```
</div>
### After Test
<div id='html-teardown'>
```html
</body>
</html>
```
</div>
</section>
## Solution
<section id='solution'>
```html
<script>
const infixToFunction = {
"+": (x, y) => x + y,
"-": (x, y) => x - y,
"*": (x, y) => x * y,
"/": (x, y) => x / y
};
const infixEval = (str, regex) =>
str.replace(regex, (_, arg1, fn, arg2) =>
infixToFunction[fn](parseFloat(arg1), parseFloat(arg2))
);
const highPrecedence = str => {
const regex = /([0-9.]+)([*\/])([0-9.]+)/;
const str2 = infixEval(str, regex);
return str === str2 ? str : highPrecedence(str2);
};
const spreadsheetFunctions = {
"": x => x
};
const applyFn = str => {
const noHigh = highPrecedence(str);
const infix = /([0-9.]+)([+-])([0-9.]+)/;
const str2 = infixEval(noHigh, infix);
const regex = /([a-z]*)\(([0-9., ]*)\)(?!.*\()/i;
const toNumberList = args => args.split(",").map(parseFloat);
const applyFunction = (fn, args) =>
spreadsheetFunctions[fn.toLowerCase()](toNumberList(args));
return str2.replace(
regex,
(match, fn, args) =>
spreadsheetFunctions.hasOwnProperty(fn.toLowerCase()) ? applyFunction(fn, args) : match
);
};
const range = (start, end) =>
start > end ? [] : [start].concat(range(start + 1, end));
const charRange = (start, end) =>
range(start.charCodeAt(0), end.charCodeAt(0)).map(x =>
String.fromCharCode(x)
);
const evalFormula = x => {
const rangeRegex = /([A-J])([1-9][0-9]?):([A-J])([1-9][0-9]?)/gi;
const rangeFromString = (n1, n2) => range(parseInt(n1), parseInt(n2));
const elemValue = n => c => document.getElementById(c + n).value;
const addChars = c1 => c2 => n => charRange(c1, c2).map(elemValue(n));
const fn = elemValue("1");
return fn("A");
};
</script>
```
</section>