Skip to content
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

Implement power function and UI changes #27

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
58 changes: 15 additions & 43 deletions .instructions/2. core exercises.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,56 +39,28 @@ Let's start by running the application to learn what it does.

**TO DO** - You've been asked to add a new feature to the calculator application.

### Adding the buttons to the calculator UI

1. Open the ```public/index.html``` file in the editor window.

2. Scroll down to where you see the ```<!-- TODO: Buttons -->``` comment
### Adding the power function to the backend

3. Add a new line below this comment and type the following two lines. You should see GitHub Copilot start to autocomplete the second line as you type. When you see this, just press ```TAB``` to accept the completion.
1. Open the `api/controller.js` file in the editor window.
2. Locate the `operations` object.
3. Add a new property to the `operations` object for the power operation. Use the key `'power'` and for the value, use a function that takes two parameters `a` and `b` and returns `Math.pow(a, b)`.

``` <!-- add a button for a power (or exponential) function --> ```
``` <button class="btn" onClick="operationPressed('^')">^</button> ```

Your finished snippet should match the following.
### Adding the buttons to the calculator UI

<img width="538" alt="GitHub Copilot suggestions" src="../assets/index-html.png">
1. Open the `public/index.html` file in the editor window.
2. Scroll down to where you see the calculator buttons.
3. Add a new button for the power operation. Use the class `"btn"` for styling, set the `onClick` attribute to call `operationPressed('^')`, and use `^` as the button text.

### Adding the logic for the new features

5. Open the ```api/controller.js``` file in the editor window.

6. Scroll down to where you see the ```// TODO: Add operator``` comment

7. Press **ENTER** at the end of the line that defines the divide function.

8. Start typing the following line and notice that GitHub Copilot should start to offer code completion half way through the word "power" as you're typing. Press **TAB** to accept the suggestion.

```'power': function(a, b) { return Math.pow(a, b) },```

9. Open the ```public/client.js``` file in the editor window.

10. Scroll down to where you see the ```// TODO: Add operator``` comment (Line 22)

11. Move your cursor to the end of the line 35 (to the right of ```break;``` and press **ENTER**.

GitHub Copilot should display ghost text suggesting the code shown in the following screenshot. Press **TAB** to accept the suggestion.

<img width="353" alt="GitHub Copilot suggestions" src="../assets/case-suggestion.png">

12. Press **ENTER** at the end of the line, then accept the next two lines Copilot suggests.

Your completed addition should match the following.

<img width="376" alt="GitHub Copilot suggestions" src="../assets/Add-operator-completed.png">

13. Press ```CTRL + ` ``` to open the terminal window in VS Code.

14. Enter ```npm start``` in the terminal window and press **ENTER** to run the application.

15. You should test the new button by clicking 3, then the "^" (power) button, then click 2. Click "=" and the result should be 9.
1. Open the `public/client.js` file in the editor window.
2. Locate the `calculate` function.
3. Add a case for the power operation in the switch statement. When the operation is `'^'`, set the URI to include `"?operation=power"`.

16. Close the browser window, return to the Terminal window in Codespaces and press ```CTRL+C``` to terminate the application.
4. Press ```CTRL + ` ``` to open the terminal window in VS Code.
5. Enter ```npm start``` in the terminal window and press **ENTER** to run the application.
6. You should test the new button by clicking 3, then the "^" (power) button, then click 2. Click "=" and the result should be 9.
7. Close the browser window, return to the Terminal window in Codespaces and press ```CTRL+C``` to terminate the application.

**Success**, you have enhanced the calculator application using GitHub Copilot!

Expand Down
17 changes: 17 additions & 0 deletions .instructions/3. challenge exercises.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ Now you've had an opportunity to get started using GitHub Copilot, we have a num

</details>

<details>
<summary>Challenge #4 - Implementing Unit Tests for the Power Function</summary>

### Implementing Unit Tests for the Power Function

Now that you have added a power function to both the backend and the frontend of the calculator application, it's important to ensure its correctness through unit testing.

1. Open the `test/arithmetic.test.js` file in your editor.
2. Add unit tests to verify the power function works as expected. Consider testing for:
- Raising a number to the power of another number.
- Raising a number to the power of 0.
- Raising a number to a negative power.
- Edge cases that you think are necessary to ensure the power function's reliability.

Remember, the goal is to use GitHub Copilot to assist you in writing these tests. See how much of the test implementation Copilot can handle for you.

</details>

#### What's next?

Expand Down
2 changes: 1 addition & 1 deletion api/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ exports.calculate = function(req, res) {
res.json({ error: err.message });
});

// TODO: Add operator
var operations = {
'add': function(a, b) { return Number(a) + Number(b) },
'subtract': function(a, b) { return a - b },
'multiply': function(a, b) { return a * b },
'divide': function(a, b) { return a / b },
'power': function(a, b) { return Math.pow(a, b) } //,
};

if (!req.query.operation) {
Expand Down
7 changes: 5 additions & 2 deletions public/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var operation = null;
function calculate(operand1, operand2, operation) {
var uri = location.origin + "/arithmetic";

// TODO: Add operator
// Handle the power operation in the switch statement
switch (operation) {
case '+':
uri += "?operation=add";
Expand All @@ -33,6 +33,9 @@ function calculate(operand1, operand2, operation) {
case '/':
uri += "?operation=divide";
break;
case '^': // Add a case for the power operation in the calculate function
uri += "?operation=power";
break;
default:
setError();
return;
Expand Down Expand Up @@ -138,7 +141,7 @@ document.addEventListener('keypress', (event) => {
numberPressed(event.key);
} else if (event.key == '.') {
decimalPressed();
} else if (event.key.match(/^[-*+/]$/)) {
} else if (event.key.match(/^[-*+/^]$/)) { // Updated to include '^' for power operation
operationPressed(event.key);
} else if (event.key == '=') {
equalPressed();
Expand Down
2 changes: 2 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
<button class="btn" onClick="equalPressed()">=</button>

<!-- TODO: Buttons -->
<!-- Add a button for the power operation in the calculator UI
<button class="btn" onClick="operationPressed('^')">^</button>
</div>
</div>
</div>
Expand Down
Loading