Skip to content

Feat/homework 06: Expression Calculator Function Created. #279

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/check_lesson_06_pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Check Lesson 06 Pull Request

on:
pull_request:
branches: [ "main" ]
paths:
- "lesson_06/expression/**"

jobs:
build:

runs-on: ubuntu-latest
permissions:
contents: read

steps:
- uses: actions/checkout@v4

- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'

- name: Build Shared Lib with Node.js
working-directory: ./lib/javascript/codedifferently-instructional
run: npm ci

- name: Build Lesson 06 with Node.js
working-directory: ./lesson_06/expression
run: |
npm ci
npm run check
32 changes: 32 additions & 0 deletions .github/workflows/check_lesson_06_quiz.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Check Lesson 06 Quiz Pull Request

on:
pull_request:
branches: [ "main" ]
paths:
- "lesson_06/quiz/**"

jobs:
build:

runs-on: ubuntu-latest
permissions:
contents: read

steps:
- uses: actions/checkout@v4

- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'

- name: Build Shared Lib with Node.js
working-directory: ./lib/javascript/codedifferently-instructional
run: npm ci

- name: Build Lesson 06 Quiz with Node.js
working-directory: ./lesson_06/quiz
run: |
npm ci
npm run check
15 changes: 15 additions & 0 deletions .github/workflows/check_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ on:
- "lib/**"
- "lesson_02/quiz/**"
- "lesson_03/quiz/**"
- "lesson_06/quiz/**"
- "lesson_06/expression/**"
jobs:
build:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -71,3 +73,16 @@ jobs:
npm ci
npm run compile
npm run lint

- name: Build Lesson 06 Quiz with Node.js
working-directory: ./lesson_06/quiz
run: |
npm ci
npm run compile
npm run lint

- name: Build Lesson 06 Expression with Node.js
working-directory: ./lesson_06/expression
run: |
npm ci
npm run compile
1 change: 1 addition & 0 deletions lesson_06/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ HW_VERSION=D
```
2. Run the program to determine the expression you must implement.
```bash
npm install
npm run compile
npm start
```
Expand Down
2 changes: 1 addition & 1 deletion lesson_06/expression/.env.test
Original file line number Diff line number Diff line change
@@ -1 +1 @@
HW_VERSION=your assigned version here
HW_VERSION=F
19 changes: 16 additions & 3 deletions lesson_06/expression/src/expression_calculator.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
export class ExpressionCalculator {
/** Returns a calculation involving a, b, c, d, and e */
calculate(a: number, b: number, c: number, d: number, e: number): number {
// Implement your code here to return the correct value.
return 0;

add(a: number, b: number): number {
return a + b;
}

multiply(a: number, b: number): number {
return a * b;
}

divide(a: number, b: number): number {
if (b === 0) throw new Error("Cannot divide by zero");
return a / b;
}

pow(base: number, exponent: number): number {
return Math.pow(base, exponent);
}

calculate(a: number, b: number, c: number, d: number, e: number): number {
return this.multiply(a, this.divide(this.pow(this.add(b, c), d), e));
}
}
33 changes: 17 additions & 16 deletions lesson_06/expression/src/functions.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
export const FUNCTIONS: Record<string, Function> = Object.freeze({
A: (a: number, b: number, c: number, d: number, e: number) =>
((a + b) * c) / Math.pow(d, e),
B: (a: number, b: number, c: number, d: number, e: number) =>
Math.pow(a + b, c) / (d * e),
C: (a: number, b: number, c: number, d: number, e: number) =>
a / b + c * Math.pow(d, e),
D: (a: number, b: number, c: number, d: number, e: number) =>
(a * b) / Math.pow(c + d, e),
E: (a: number, b: number, c: number, d: number, e: number) =>
Math.pow((a + b) / c, d) * e,
F: (a: number, b: number, c: number, d: number, e: number) =>
(a * Math.pow(b + c, d)) / e,
G: (a: number, b: number, c: number, d: number, e: number) =>
((Math.pow(a, b) + c) * d) / e,
});
export const FUNCTIONS: Record<string, (...args: number[]) => number> =
Object.freeze({
A: (a: number, b: number, c: number, d: number, e: number) =>
((a + b) * c) / Math.pow(d, e),
B: (a: number, b: number, c: number, d: number, e: number) =>
Math.pow(a + b, c) / (d * e),
C: (a: number, b: number, c: number, d: number, e: number) =>
a / b + c * Math.pow(d, e),
D: (a: number, b: number, c: number, d: number, e: number) =>
(a * b) / Math.pow(c + d, e),
E: (a: number, b: number, c: number, d: number, e: number) =>
Math.pow((a + b) / c, d) * e,
F: (a: number, b: number, c: number, d: number, e: number) =>
(a * Math.pow(b + c, d)) / e,
G: (a: number, b: number, c: number, d: number, e: number) =>
((Math.pow(a, b) + c) * d) / e,
});
2 changes: 1 addition & 1 deletion lesson_06/expression/src/lesson6.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("Lesson6Test", () => {
const c = 3;
const d = 4;
const e = 5;
const expected = FUNCTIONS[process.env.HW_VERSION!].call(
const expected = FUNCTIONS[process.env.HW_VERSION || ""].call(
null,
...[a, b, c, d, e],
);
Expand Down
2 changes: 1 addition & 1 deletion lesson_06/expression/src/lesson6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const rl = createInterface({
});

const main = async () => {
const testFunction = FUNCTIONS[process.env.HW_VERSION!];
const testFunction = FUNCTIONS[process.env.HW_VERSION || ""];
if (testFunction === undefined) {
console.log("HW_VERSION version not set in config. Exiting...");
process.exit(1);
Expand Down
8 changes: 4 additions & 4 deletions lesson_06/expression/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export const askQuestion = (
});
};

export const getFunctionBody = (f: Function) => {
const fString = f.toString().split("=>").pop()!;
return beautify(fString);
export const getFunctionBody = (f: () => unknown) => {
const fString = f.toString().split("=>").pop();
return beautify(fString || "");
};

export const printFormualaWithValues = (
Expand All @@ -34,7 +34,7 @@ export const printFormualaWithValues = (
["e", String(e)],
["pow", "Math.pow"],
).reduce(
(acc: string, param: string[]) => acc.replace(param[0]!, param[1]!),
(acc: string, param: string[]) => acc.replace(param[0], param[1]),
functionBody,
);
console.log(`\nNow computing the value of ${formula}`);
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.