mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2025-12-22 03:26:02 -05:00
Co-authored-by: Jessica Wilkins <67210629+jdwilkin4@users.noreply.github.com> Co-authored-by: Krzysztof G. <60067306+gikf@users.noreply.github.com>
168 lines
4.3 KiB
Markdown
168 lines
4.3 KiB
Markdown
---
|
|
id: 646d3d037872fbbae0a8ec0e
|
|
title: Step 65
|
|
challengeType: 0
|
|
dashedName: step-65
|
|
---
|
|
|
|
# --description--
|
|
|
|
Object values do not have to be primitive types, like a string or a number. They can also be functions.
|
|
|
|
Give your `infixToFunction` object a `+` property. That property should be a function that takes an `x` and `y` parameter and implicitly returns the sum of those two parameters.
|
|
|
|
Because `+` is not alphanumeric, you'll need to wrap it in quotes for your property.
|
|
|
|
# --hints--
|
|
|
|
Your `infixToFunction` object should have a `+` property.
|
|
|
|
```js
|
|
assert.property(infixToFunction, '+');
|
|
```
|
|
|
|
Your `+` property should be a function.
|
|
|
|
```js
|
|
assert.isFunction(infixToFunction['+']);
|
|
```
|
|
|
|
Your `+` function should use arrow syntax.
|
|
|
|
```js
|
|
assert.match(code, /const\s+infixToFunction\s*=\s*\{\s*('|"|`)\+\1\s*:\s*\(/);
|
|
```
|
|
|
|
Your `+` function should have `x` as its first parameter.
|
|
|
|
```js
|
|
assert.match(code, /const\s+infixToFunction\s*=\s*\{\s*('|"|`)\+\1\s*:\s*\(\s*x/);
|
|
```
|
|
|
|
Your `+` function should have `y` as its second parameter.
|
|
|
|
```js
|
|
assert.match(code, /const\s+infixToFunction\s*=\s*\{\s*('|"|`)\+\1\s*:\s*\(\s*x\s*,\s*y\s*\)/);
|
|
```
|
|
|
|
Your `+` function should use an implicit return.
|
|
|
|
```js
|
|
assert.notMatch(code, /const\s+infixToFunction\s*=\s*\{\s*('|"|`)\+\1\s*:\s*\(\s*x\s*,\s*y\s*\)\s*\{/);
|
|
```
|
|
|
|
Your `+` function should return the sum of `x` and `y`.
|
|
|
|
```js
|
|
assert.equal(infixToFunction['+'](1, 2), 3);
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```html
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
<link rel="stylesheet" href="./styles.css" />
|
|
<title>Functional Programming Spreadsheet</title>
|
|
</head>
|
|
<body>
|
|
<div id="container">
|
|
<div></div>
|
|
</div>
|
|
<script src="./script.js"></script>
|
|
</body>
|
|
</html>
|
|
```
|
|
|
|
```css
|
|
#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;
|
|
}
|
|
```
|
|
|
|
```js
|
|
--fcc-editable-region--
|
|
const infixToFunction = {
|
|
|
|
}
|
|
--fcc-editable-region--
|
|
|
|
const isEven = num => num % 2 === 0;
|
|
const sum = nums => nums.reduce((acc, el) => acc + el, 0);
|
|
const average = nums => sum(nums) / nums.length;
|
|
|
|
const median = nums => {
|
|
const sorted = nums.slice().sort((a, b) => a - b);
|
|
const length = sorted.length;
|
|
const middle = length / 2 - 1;
|
|
return isEven(length)
|
|
? average([sorted[middle], sorted[middle + 1]])
|
|
: sorted[Math.ceil(middle)];
|
|
}
|
|
|
|
const spreadsheetFunctions = {
|
|
sum,
|
|
average,
|
|
median
|
|
}
|
|
|
|
const range = (start, end) => Array(end - start + 1).fill(start).map((element, index) => element + index);
|
|
const charRange = (start, end) => range(start.charCodeAt(0), end.charCodeAt(0)).map(code => String.fromCharCode(code));
|
|
|
|
const evalFormula = (x, cells) => {
|
|
const idToText = id => cells.find(cell => cell.id === id).value;
|
|
const rangeRegex = /([A-J])([1-9][0-9]?):([A-J])([1-9][0-9]?)/gi;
|
|
const rangeFromString = (num1, num2) => range(parseInt(num1), parseInt(num2));
|
|
const elemValue = num => character => idToText(character + num);
|
|
const addCharacters = character1 => character2 => num => charRange(character1, character2).map(elemValue(num));
|
|
const rangeExpanded = x.replace(rangeRegex, (_match, char1, num1, char2, num2) => rangeFromString(num1, num2).map(addCharacters(char1)(char2)));
|
|
const cellRegex = /[A-J][1-9][0-9]?/gi;
|
|
const cellExpanded = rangeExpanded.replace(cellRegex, match => idToText(match.toUpperCase()));
|
|
}
|
|
|
|
window.onload = () => {
|
|
const container = document.getElementById("container");
|
|
const createLabel = (name) => {
|
|
const label = document.createElement("div");
|
|
label.className = "label";
|
|
label.textContent = name;
|
|
container.appendChild(label);
|
|
}
|
|
const letters = charRange("A", "J");
|
|
letters.forEach(createLabel);
|
|
range(1, 99).forEach(number => {
|
|
createLabel(number);
|
|
letters.forEach(letter => {
|
|
const input = document.createElement("input");
|
|
input.type = "text";
|
|
input.id = letter + number;
|
|
input.ariaLabel = letter + number;
|
|
input.onchange = update;
|
|
container.appendChild(input);
|
|
})
|
|
})
|
|
}
|
|
|
|
const update = event => {
|
|
const element = event.target;
|
|
const value = element.value.replace(/\s/g, "");
|
|
if (!value.includes(element.id) && value.startsWith('=')) {
|
|
|
|
}
|
|
}
|
|
```
|