106 lines
1.7 KiB
Markdown
106 lines
1.7 KiB
Markdown
![]() |
---
|
||
|
id: 5d792533aa6443215c9b16bf
|
||
|
title: Part 21
|
||
|
challengeType: 0
|
||
|
dashedName: part-21
|
||
|
---
|
||
|
|
||
|
# --description--
|
||
|
|
||
|
Now, assign the result of calling `infixEval` with `str` and `regex` to `str2`. Return `str2`.
|
||
|
|
||
|
# --hints--
|
||
|
|
||
|
See description above for instructions.
|
||
|
|
||
|
```js
|
||
|
assert(highPrecedence('7*6') === '42' && highPrecedence('50/25') === '2');
|
||
|
```
|
||
|
|
||
|
# --seed--
|
||
|
|
||
|
## --before-user-code--
|
||
|
|
||
|
```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>
|
||
|
```
|
||
|
|
||
|
## --after-user-code--
|
||
|
|
||
|
```html
|
||
|
</body>
|
||
|
</html>
|
||
|
```
|
||
|
|
||
|
## --seed-contents--
|
||
|
|
||
|
```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.]+)/;
|
||
|
return str;
|
||
|
};
|
||
|
|
||
|
|
||
|
</script>
|
||
|
```
|
||
|
|
||
|
# --solutions--
|
||
|
|
||
|
```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 str2;
|
||
|
};
|
||
|
</script>
|
||
|
```
|